Comments (10)
Fixed in version 1.5.0.
from axios-hooks.
Can you post a realistic source code example?
from axios-hooks.
Here's a little more detail. If you need an even deeper example, I can try to provide that later tonight.
const user = useSelector((state) => state.auth.username);
const [, onReadMetadata] = useAxios({ url: "/api/customer/readMetadata", method: "GET" }, { manual: true });
useEffect(() => {
console.log("--------------this ran---------------");
onReadMetadata({
params: { user: user },
transformResponse: [
(response) => {
setMetadata(JSON.parse(response));
},
],
});
}, [user, timestamp, onReadMetadata]);
Our application needs to update the a table holding the metadata every time either the user or a certain timestamp variable changes. The timestamp is changed by another component and it is the way we notify this component that the metadata needs to be get an update from the back-end via the axios-hooks API call.
The React hooks dependencies say that we should also include onReadMetadata() as a dependency. However, including this as a dependency causes hundreds of re-renders per second. So, for some reason it sees this function as constantly changing. I've verified that the other dependencies are not the cause and its definitely caused by the execution function from the hook.
The typical solution offered by the React documents for this would be to wrap onReadMetadata() in its own function that includes useCallback(). Something like this:
const updateMetadata = useCallback(() => {
console.log("--------------this ran---------------");
onReadMetadata({
params: { user: user },
transformResponse: [
(response) => {
setMetadata(JSON.parse(response));
},
],
});
}, [ onReadMetadata]);
However, memoizing this does not fix the issue and it still results in this being re-rendered tons of times per second. Thus, it would seem that the onReadMetadata() execution function coming from the hook is constantly changing. Basically, it makes including this execution function in useEffect() or useCallback() difficult to utilize while following the general guidelines of hooks. I would think that it shouldn't be changing constantly and really should be maintaining the same state though. I will look through the source code today and see if I can find a possible cause. Perhaps something in the source needs to be memoized and only returned via the hook on change?
Thanks, let me know if you need more details. Hopefully this was sufficient information.
from axios-hooks.
Thanks for the details, I can see the issue now. Yes, the function returned by the hook is indeed a new function every time the hook is executed, hence why you're experiencing this behavior.
I can try to find a way to prevent this from happening. In the meanwhile, have you tried ignoring the eslint rule and omit the onReadMetadata
function from the list of dependencies in your useEffect
hook?
from axios-hooks.
Thank you, I appreciate you taking a look at this.
Yes, I have tried excluding it from the dependencies and it does work as desired. So thankfully there is a working solution and this won't by any means be breaking the application. Long term, it would be nice to follow the general guidelines as I think there probable is some type of performant, memoized solution for this (though I don't have that answer at this time).
Let me know if you have any other questions. If I get time over the next week or so, I'll take a look to see if I can offer a potential solution for this.
from axios-hooks.
Yes let's use that as a mitigation and meanwhile let me check what it would take to make that function keep its reference rather than being created every time.
from axios-hooks.
@davidwilson3 I published a prerelease version where I tried to address this issue. It's available as version 1.5.0.0-0, can you try it out and let me know if it fixes the issue?
from axios-hooks.
@simoneb Sure thing, I'll check as soon as I get a chance. Thanks for looking at this so quickly.
from axios-hooks.
This appears to have fixed the issue. I tested it a few different ways and it now works great with useEffect and useCallback.
Thank you. Feel free to close this issue.
from axios-hooks.
Thanks for confirming. I will release a non-prerelease version with this fix soon.
from axios-hooks.
Related Issues (20)
- Release pending!
- Refetch "expression is not callable" HOT 2
- Why I'm getting 2 rendered using this hook, when component mounts? HOT 9
- Release pending!
- Prevent CanceledError and redundant HTTP calls for identical request data HOT 1
- Support for API polling HOT 1
- type error HOT 9
- Uncaught TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator)) HOT 1
- How to configure SSR with NextJS? HOT 3
- "Uncaught runtime errors: Cannot read properties of undefined (reading 'data')" after upgrading to CRA 5.0.1 & Node 18 HOT 6
- Not usable within the US? HOT 1
- Error from a dynamically loaded uri HOT 3
- Release pending!
- Release pending!
- useAxios hook sending in 2 requests, the first is cancelled immediately HOT 3
- Release pending! HOT 3
- Release pending!
- Release pending!
- Type error: MouseEvent using refetch and provided example from docs HOT 2
- Still an issue with both `executeGet` and `execute` methods when executed manually HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from axios-hooks.