Relative Content

Tag Archive for javascriptreactjsnext.jsaccess-tokenrefresh-token

How to call API recursively when access token expired without using useEffect? React Next.js

I’m building the client page with Next.js. I have function handleSubmit that calls API to create a new product. It is called when the user clicks a button. In handleSubmit, I check if the response returns a 403 error, which means the access token has expired or is invalid, and if there is, I will call the refresh token API (updateTokens) and store the new access token along with the refresh token in local storage. I want to recursively call the handleSubmit function again after calling updateTokens. However, Typescript complains about the even parameter, saying Argument of type ‘FormEvent’ is not assignable to parameter of type ‘ChangeEvent’. And I also think the function will stuck in an infinite loop.