use-persisted-state | custom React Hook that provides a multi-instance | Frontend Utils library
kandi X-RAY | use-persisted-state Summary
kandi X-RAY | use-persisted-state Summary
A custom React Hook that provides a multi-instance, multi-tab/browser shared and persistent state.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of use-persisted-state
use-persisted-state Key Features
use-persisted-state Examples and Code Snippets
Community Discussions
Trending Discussions on use-persisted-state
QUESTION
In Main.tsx, I have been using useApi(opts,payload), which is a hook I made that is subscribed to changes in payload. It will make an API call whenever that variable is changed.
When I use usApi() in Main.tsx (as I have commented out), everything works fine.
It is only when I moved this logic to another functional component (Auth.tsx), and try to call it within Main.tsx that I encounter a problem. The problem is that it seems to attempt to make an API call in an infinite loop, but never throws an error.
What is happening here?
Main.tsx
...ANSWER
Answered 2021-Sep-12 at 23:22We've spoken a little bit on chat in order to find out a little bit more about what is going on here. I'll edit your question in a moment to take out the irrelevant code so that this is easier to understand for anyone stumbling on the question.
Your original code did the following:
- Instantiated an object (
defaultProps
) within a functional component - Passed that object in as the
default
for auseState()
call - Passed the state value into your hook, which was then used in the dependency array of a
useEffect()
hook
Your refactor changed it so you were directly passing the defaultProps reference into your hook without going through a useState()
hook as before.
An important thing to understand about React components is that although their execution pattern is somewhat predictable, it is something you have no control over as a developer and so you should assume when writing your components that they may be re-rendered at any time.
Within a functional component, each re-render is a new function execution with a new scope. If you instantiate an object within this scope, that's a new reference variable pointing to a different area of memory each time
The problem with then using this within the dependency array of a useEffect()
hook is that since this is a new variable on each render, it means that each time the component is re-rendered, the useEffect hook will see a different reference, and cause the effect callback to be executed again. This is not normally the intended behaviour.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install use-persisted-state
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page