-
Notifications
You must be signed in to change notification settings - Fork 130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refetch function throwing: Cannot read property 'token' of undefined #250
Comments
Which version are you using? Can you try to reproduce the issue in this codesandbox? |
I just fixed it updating Axios to 0.19.2 (as your codesandbox example). Thanks! |
We're currently seeing the same behaviour on the following versions: It seems to trigger instantly when navigating away from a component that has a I'll see if I can get repo in the code sandbox. |
I've patched it by changing this line But I'm not sure this is valid thinking. 🤔 |
@MichielDeMey can you provide a repro? If the token cancels, it must mean that the useEffect has triggered, hence there must be a token available. |
Yeah It is happening again.. It is the same line as @MichielDeMey mentioned.
My guess is that the hook is not ready to receive the refetch function when a child triggers a prop function on mount. |
@LuisCarrilloR thanks for the repro, I'll have a look. |
@LuisCarrilloR @MichielDeMey I published a prerelease version [email protected] which contains a simple change that should address this problem. See the linked PR for what the change is. Can you check if it solves the issue in your case? |
Here's an example I put together. Note that the original repro cannot possibly work, because if you fetch data in the child component when it mounts using a
|
Thanks for the update! I will try to find a way to reproduce it. |
Here you can find another example, it breaks when using the class component lifecycle method. |
Thanks @LuisCarrilloR yeah that makes sense. The quick fix I tried was to use I guess the solution is not as simply as changing the hook that's used to create the cancel token, so I'll come up with a different approach. Thanks for your help troubleshooting this so far. |
Hello @simoneb, I have found a couple possible solutions. Let me know your thought or if it breaks something else.
|
@simoneb Meanwhile, I can confirm that the prerelease fixes my specific issue. 👍 |
Thanks @MichielDeMey. That won't be the final solution, I'll have to come up with a more robust approach. Thanks @LuisCarrilloR, I started thinking about about a more generic approach, initializing the token together with the ref will be probably part of the solution. |
@MichielDeMey @LuisCarrilloR I published a prerelease version The rationale I followed is that any request triggered by the hook should cancel any other request, whether it's the useEffect hook used by axios-hooks or a manual request. Can you please try it out and let me know if you find any additional issues? |
Nice thanks! I am going to start moving some requests and I will let you know if something breaks. |
fixes #250 This will make it possible to pass the refetch function down to children component and let them invoke it, without erroring on a missing token. New behavior: cancelation errors are thrown for requests generated by the `refetch` function. This is more consistent with how response and other errors are returned when fetching manually.
fixes #250 This will make it possible to pass the refetch function down to children component and let them invoke it, without erroring on a missing token. New behavior: cancelation errors are thrown for requests generated by the `refetch` function. This is more consistent with how response and other errors are returned when fetching manually.
This is now addresses in 1.11.0. Please reopen this issue if you still encounter problems. |
For me the cancelToken option now does not work anymore - calling source.cancel does not cancel the request anymore. I need to cancel a request manually even if no new request is started (what axios-hooks now does automatically). |
@chickenwing Which version are you using and can you provide a repro please? |
Hello,
I created a custom hook using:
Then I tried to use it with the option manual as it is going to be triggered just when a handler function is called:
I try to call it in the handler as show below:
The problem is that when I call it on my handler it throws an error: Uncaught (in promise) TypeError: Cannot read property 'token' of undefined
The text was updated successfully, but these errors were encountered: