Skip to content

Memoize navigation action functions #7631

@slorber

Description

@slorber

Related to this hooks issue: react-navigation/hooks#3 (comment)

People are trying to use the navigate() function in useEffect, and try to comply to eslint dependency rules, unfortunately the navigation function provided by the NavigationContext changes often, and lead to unwanted effect re-executions on each screen change.

function ReactComponent() {
  const {navigate} = useNavigation();

  useEffect(() => {
      if (user) {
          navigate(...)
      }
  }, [user, navigate])

 return null
}

Not sure exactly how to fix this, but it would be nice if the navigation actions would be memoized to avoid being recreated.

Could it make sense to cache per route key the result of getNavigationActionCreators / router.getActionCreators ?

https://github.com/react-navigation/core/blob/e514a61f9314e8d771231de9d3a4ebf64f661003/src/getNavigation.js#L46
https://github.com/react-navigation/core/blob/e514a61f9314e8d771231de9d3a4ebf64f661003/src/routers/getNavigationActionCreators.js

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions