File tree Expand file tree Collapse file tree 2 files changed +66
-7
lines changed
packages/toolkit/src/query Expand file tree Collapse file tree 2 files changed +66
-7
lines changed Original file line number Diff line number Diff line change @@ -1035,8 +1035,6 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
10351035 skipPollingIfUnfocused,
10361036 } )
10371037
1038- const lastRenderHadSubscription = useRef ( false )
1039-
10401038 // TODO: Change this to `useRef<QueryActionCreatorResult<any>>(undefined)` after upgrading to React 19.
10411039 /**
10421040 * @todo Change this to `useRef<QueryActionCreatorResult<any>>(undefined)` after upgrading to React 19.
@@ -1059,11 +1057,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
10591057 }
10601058
10611059 const subscriptionRemoved =
1062- ! currentRenderHasSubscription && lastRenderHadSubscription . current
1063-
1064- usePossiblyImmediateEffect ( ( ) => {
1065- lastRenderHadSubscription . current = currentRenderHasSubscription
1066- } )
1060+ ! currentRenderHasSubscription && promiseRef . current !== undefined
10671061
10681062 usePossiblyImmediateEffect ( ( ) : void | undefined => {
10691063 if ( subscriptionRemoved ) {
Original file line number Diff line number Diff line change @@ -895,6 +895,71 @@ describe('hooks tests', () => {
895895 status : 'uninitialized' ,
896896 } )
897897 } )
898+
899+ test ( 'reset after unmount/remount' , async ( ) => {
900+ const user = userEvent . setup ( )
901+
902+ function QueryComponent ( ) {
903+ const { isLoading, data } = api . endpoints . getUser . useQuery ( 1 )
904+
905+ if ( isLoading ) {
906+ return < p > Loading...</ p >
907+ }
908+
909+ return < p > { data ?. name } </ p >
910+ }
911+
912+ function Wrapper ( ) {
913+ const [ open , setOpen ] = useState ( true )
914+
915+ const handleRerender = ( ) => {
916+ setOpen ( false )
917+ setTimeout ( ( ) => {
918+ setOpen ( true )
919+ } , 1000 )
920+ }
921+
922+ const handleReset = ( ) => {
923+ storeRef . store . dispatch ( api . util . resetApiState ( ) )
924+ }
925+
926+ return (
927+ < >
928+ < button onClick = { handleRerender } aria-label = "Rerender component" >
929+ Rerender
930+ </ button >
931+ { open ? (
932+ < div >
933+ < button onClick = { handleReset } aria-label = "Reset API state" >
934+ Reset
935+ </ button >
936+
937+ < QueryComponent />
938+ </ div >
939+ ) : null }
940+ </ >
941+ )
942+ }
943+
944+ render ( < Wrapper /> , { wrapper : storeRef . wrapper } )
945+
946+ await user . click (
947+ screen . getByRole ( 'button' , { name : / R e r e n d e r c o m p o n e n t / i } ) ,
948+ )
949+ await waitFor ( ( ) => {
950+ expect ( screen . getByText ( 'Timmy' ) ) . toBeTruthy ( )
951+ } )
952+
953+ await user . click (
954+ screen . getByRole ( 'button' , { name : / r e s e t a p i s t a t e / i } ) ,
955+ )
956+ await waitFor ( ( ) => {
957+ expect ( screen . queryByText ( 'Loading...' ) ) . toBeNull ( )
958+ } )
959+ await waitFor ( ( ) => {
960+ expect ( screen . getByText ( 'Timmy' ) ) . toBeTruthy ( )
961+ } )
962+ } )
898963 } )
899964
900965 test ( 'useQuery refetch method returns a promise that resolves with the result' , async ( ) => {
You can’t perform that action at this time.
0 commit comments