question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Infinite re-rendering with hook

See original GitHub issue

Current behavior

I assume this behaviour is due to the functions coming back being new instances each time, but if I use the functions returned from the hook in a useEffect and add them to the dependency array, the useEffect fires infinitely. I can solve this by either not including them in the dependency array (not good practice) or wrapping them in a useCallback (not extraordinarily elegant, but better than the first option).

Expected behavior

The functions returned from the hook should be able to be placed in a dependency array of a useEffect without issue.

Repro steps

  1. Grab the getItem function (for example) from the hook.
  2. Create a useEffect hook that uses getItem.
  3. Do something obvious in the effect that will signal each time it is called (such as a console.warn).
  4. Add getItem to the dependency array of the useEffect.
  5. Run the code.

Environment

  • Async Storage version: ^1.7.1
  • React-Native version: 0.59.5
  • Platform tested: Android
  • Logs/Error that are relevant: N/A

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:17 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
gustavopchcommented, Apr 23, 2020

Wouldn’t wrapping the functions with useCallback solve this? https://github.com/react-native-community/async-storage/blob/e7e3a4fc8a662ccadfea16bedd645b4fdc20d23b/lib/hooks.js#L24-L27 I mean:

    getItem: useCallback((...args) => AsyncStorage.getItem(key, ...args), [key]),
    setItem: useCallback((...args) => AsyncStorage.setItem(key, ...args), [key]),
    mergeItem: useCallback((...args) => AsyncStorage.mergeItem(key, ...args), [key]),
    removeItem: useCallback((...args) => AsyncStorage.removeItem(key, ...args), [key]),
0reactions
github-actions[bot]commented, Apr 28, 2022

This issue has been marked as stale due to inactivity. Please respond or otherwise resolve the issue within 7 days or it will be closed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React useState infinite rerender - Stack Overflow
What you need is useEffect hook with dependency array, which will keep track of supplied parameter and reacts when that parameter changes. Now ......
Read more >
Preventing infinite re-renders when using useEffect and ...
React's useEffect hook is an incredibly useful tool for fetching data, but if you're not careful, can cause infinite re-renders.
Read more >
How to solve the React useEffect Hook's infinite loop patterns
Solve the issue of infinite loops when using the useEffect Hook in React to more smoothly utilize the Hook for your app's side...
Read more >
How to Solve the Infinite Loop of React.useEffect()
useEffect() React hook manages the side-effects like fetching over the network, manipulating DOM directly, starting and ending timers.
Read more >
Too many re-renders. React limits the number of renders to ...
Having a useEffect hook that sets and re-renders infinitely. too many re renders react limits the number. Here is an example of how...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found