react-navigation / hooks

React hooks for convenient react-navigation use
https://reactnavigation.org
MIT License
576 stars 36 forks source link

Provide stable navigation actions #40

Open slorber opened 5 years ago

slorber commented 5 years ago
function ReactComponent() {
  const { navigate } = useNavigation();
  useEffect(() => {
      if (someBoolean) {
          navigate(...)
      }
  },  [someBoolean,navigate])
}

The functions like navigate/goBack etc should be able to be used in hooks / dependency arrays.

We should rather make them stable so that they don't trigger effect re-execution on every navigate action, which is not the case currently (as reported by @cmmartin here: https://github.com/react-navigation/hooks/issues/3#issuecomment-524176110)

See also https://github.com/react-navigation/core/issues/71

The core does not provide stable action functions currently, and it may be complicated to do so and guarantee that as part of the core contract.

But we can provide stabilization in this package.

slorber commented 4 years ago

@cmmartin for the record I've opened a React issue, which is a bit related to this issue (at least it's an example).

https://github.com/facebook/react/issues/16956

Will try to see which solution to adopt after having answers.

cmmartin commented 4 years ago

@slorber Thanks for writing that up. I have been struggling with the same issues. My codebase is full of violations of the hooks dependencies lint rule and I'm not sure how to resolve them. Will follow the other issue