use-api-polling icon indicating copy to clipboard operation
use-api-polling copied to clipboard

Hook doesn't clean up after redirect.

Open vlad-elagin opened this issue 4 years ago • 3 comments

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.

vlad-elagin avatar Jun 24 '20 07:06 vlad-elagin

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

lazyd3v avatar Jun 24 '20 07:06 lazyd3v

Component with polling functionality isn't rendered after redirect, so it is actually the same. But I'll try this. Thanks.

vlad-elagin avatar Jun 24 '20 07:06 vlad-elagin

Hmm. There's a test for unmount scenario, so it should work. I'll try to reproduce it in the sandbox

lazyd3v avatar Jun 24 '20 07:06 lazyd3v