react-use icon indicating copy to clipboard operation
react-use copied to clipboard

Conflict between the use createStateContext and createReducerContext hooks and exhaustive-deps lint rule

Open tamakisquare opened this issue 6 years ago • 2 comments

What is the current behavior? The functions, setState and dispatch, associated with createStateContext and createReducerContext hooks, respectively, are flagged by the exhaustive-deps lint rule when they are used in hooks such as useEffect and not being included in the dependency arrays.

Steps to reproduce it and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have extra dependencies other than react-use.

Here is a link to screenshots that showcase the issue. I took the demo code of the createStateContext hook taken from the official docs and added a simple useEffect to demonstrate the issue. Screenshots

What is the expected behavior? According to the official React docs (reference), the setState and dispatch functions returned from useState and useReducer hooks have stable identity and thus are safe to exclude them from the dependency array. In fact, the exhaustive-deps lint rule does not flag setState and dispatch functions associated with useState and useReducer hooks when they are omitted from the dependency array.

The setState and dispatch functions resulted from createStateContext and createReducerContext should be compatible with the exhaustive-deps lint rule in the same way.

A little about versions:

  • OS: MacOS 10.15.5
  • Browser (vendor and version):
  • React: v16.13.1
  • react-use: v15.3.2
  • Did this worked in the previous package version?

tamakisquare avatar Jul 22 '20 07:07 tamakisquare

The setState and dispatch functions resulted from createStateContext and createReducerContext should be compatible with the exhaustive-deps lint rule in the same way.

Is that possible to do?

dcporter avatar Jul 22 '20 08:07 dcporter

After a second thought, I think it's right how the lint rule applies as it is. Although the original useState and dispatch functions are designed to have stable identities, the useState and dispatch functions associated with createStateContext and createReducerContext are in fact context objects, which are mutable and thus cannot be viewed as stable.

tamakisquare avatar Jul 23 '20 03:07 tamakisquare