use-api-polling
use-api-polling copied to clipboard
Hook doesn't clean up after redirect.
Hello! I've encountered an issue with this hook. I need to conditionally redirect to another route depending on result of polling. It doesn't stop polling after redirect is done. Here is example of code:
` const history = useHistory();
const checkPaymentStatus = () => { return callApi("GET", "/payStatus"); };
const pollingOptions = { fetchFunc: checkPaymentStatus, initialState: 1, delay: 1000, };
const status = useAPIPolling(pollingOptions);
if (status === 4) { history.push("/"); }
if (status === 2) { history.push("/thankyou"); } `
Any ideas why it happens and how to fix it? btw thank you for this package, it would be great time-saver if not this issue.
Right now there's no functionality to stop polling without component unmount, but that's a good suggestion, I'll think how to implement it.
But I think right now you can createAPIPolling
component
const APIPolling = () => {
const history = useHistory()
const checkPaymentStatus = () => {
return callApi('GET', '/payStatus')
}
const pollingOptions = {
fetchFunc: checkPaymentStatus,
initialState: 1,
delay: 1000,
}
const status = useAPIPolling(pollingOptions)
if (status === 4) {
history.push('/')
}
if (status === 2) {
history.push('/thankyou')
}
return null
}
Then include this component only to the page where you need this polling. So, when you will be redirected to /thankyou
, APIPolling
component will not be in the tree and polling will stop
Component with polling functionality isn't rendered after redirect, so it is actually the same. But I'll try this. Thanks.
Hmm. There's a test for unmount scenario, so it should work. I'll try to reproduce it in the sandbox