Skip to content
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

Infinite re-rendering with hook #285

Open
dawsonc623 opened this issue Jan 28, 2020 · 18 comments
Open

Infinite re-rendering with hook #285

dawsonc623 opened this issue Jan 28, 2020 · 18 comments

Comments

@dawsonc623
Copy link

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
@krizzu krizzu added the LEGACY label Feb 3, 2020
@krizzu
Copy link
Member

krizzu commented Feb 3, 2020

Hey @dawsonc623

Please do not use those "hooks" - the implementation is broken and should be revisited. Here's a discussion regarding possible implementation

@dawsonc623
Copy link
Author

Hello @krizzu

Thanks for the reply. I will avoid the hook for now and use the standard API.

Do you know when the hook might be looked at again?

@krizzu
Copy link
Member

krizzu commented Feb 4, 2020

Haven't thought about specific date, but definitely should land in v2!

@krizzu krizzu removed the LEGACY label Apr 12, 2020
@gustavopch
Copy link

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]),

@arniu
Copy link

arniu commented Jun 17, 2020

In my opinion, the hook here is to paint the lily.

@github-actions
Copy link

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.

@github-actions github-actions bot added the Stale label May 18, 2021
@gustavopch
Copy link

Not stale.

@github-actions github-actions bot removed the Stale label May 19, 2021
@github-actions
Copy link

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.

@github-actions github-actions bot added the Stale label Jul 19, 2021
@gustavopch
Copy link

Not stale.

@github-actions github-actions bot removed the Stale label Jul 20, 2021
@github-actions
Copy link

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.

@github-actions github-actions bot added the Stale label Sep 19, 2021
@gustavopch
Copy link

Not stale.

@github-actions github-actions bot removed the Stale label Sep 20, 2021
@mohsinali1317
Copy link

Not stale.

@github-actions
Copy link

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.

@github-actions github-actions bot added the Stale label Dec 21, 2021
@gustavopch
Copy link

Not stale (but will likely never be solved too).

@github-actions github-actions bot removed the Stale label Dec 22, 2021
@nibblesnbits
Copy link

Still not stale.... Is this going to be addressed? Is there another library I can use?

@pke
Copy link

pke commented Feb 26, 2022

It will be fixed when PR #760 is merged

@github-actions
Copy link

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.

@github-actions github-actions bot added the Stale label Apr 28, 2022
@tido64 tido64 removed the Stale label Apr 28, 2022
@TheoOliveira
Copy link

TheoOliveira commented Mar 22, 2024

It's the work done by @pke the way you guys want to procced?. I am hitting this error as well, i might try to help with that. Any more insights, context on the issue? It would be my first issue on the project but i am hitting that without using the hook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants