astoilkov / use-local-storage-state

React hook that persists data in localStorage
MIT License
1.1k stars 41 forks source link

Setter in useEffect dependencies array always changes #44

Closed martinsantibanez closed 2 years ago

martinsantibanez commented 2 years ago

Hello, not sure if this should be taken as a bug or just a question.

Whenever I'm updating the localStorage state inside an effect, I cannot put the setter in the dependencies array. Otherwise, it causes infinite renders.

My workaround is to exclude the setter from the deps array. However, this is a bit unfortunate due to the react-hooks/exhaustive-deps eslint rule.

How do you deal with this? do you just ignore the exhaustive-deps warning?

You can see an example here: https://codesandbox.io/s/blissful-faraday-37g5wg?file=/src/App.js

Thanks!

astoilkov commented 2 years ago

I've fixed the issue. Thanks for yet another contribution!

martinsantibanez commented 2 years ago

Thanks! really look forward for this as we're soon shipping v15 to prod

astoilkov commented 2 years ago

I've released v16. You can try it out and let me know if everything works correctly.

Ganbin commented 1 year ago

@martinsantibanez Just as a question. I don't see real use case where we would explicitly need to set the setter function as a dependency except to shut up the linter.

Do I miss something?

martinsantibanez commented 1 year ago

@Ganbin Yep, just to shut up the linter I think. Unless the setter changes, which I'm not sure about