Description
I am struggling with a problem when using polling. Seems that even after my component dismount, the polling keeps happening and never stops. It is a very hard problem to reproduce, but I find that when we have the StrictMode around the app, the problems happen all the time. To be able to create a reproducible example, I changed the relay-hook-example/todo of this repository here: gtkatakura/relay-hooks#polling-problem.
- I added support to cancel the request into the fetchQuery:
const controller = new AbortController();
fetch('http://localhost:3000/graphql', {
signal: controller.signal,
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: operation.text,
variables,
}),
})
.then(response => response.json())
.then(data => {
if (data.errors) {
sink.error(data.errors);
return;
}
sink.next(data);
sink.complete();
});
return () => {
controller.abort();
};
-
I changed the polling to 5 seconds and added a setTimeout into the back-end to resolve the response into 3 seconds
-
I added a button to hide and show the component on the screen
-
I enabled the StrictMode
So after these changes, you just need to run the todo example and click into hide
to dismount the component with the user query. You will see that after the dismount, it will keep polling the query. And if you click into hide
when is happening the fetch, it doesn't cancel the request. If you remove the StrictMode, it will cancel the request properly.