Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use withNavigation in react-navigation v5?

I have a nested component, and I want to use withNavigation in the nested component in react-navigation v5.

like image 462
learner62 Avatar asked May 25 '20 05:05

learner62


People also ask

How do you use withNavigation in react navigation 5?

You can get access to the root navigation object through a ref and pass it to the RootNavigation which we will later use to navigate. In the next step, we define RootNavigation, which is a simple module with functions that dispatch user-defined navigation actions.

How do you use useNavigation in react?

Step 1: To start with, create a React application using the following command: npx create-react-app <project_name>; Step 2: Install the latest version of react-router-dom in the React application by the following. Project Structure: Create a folder named components in the src folder and add files Home.

How do you use useNavigation in class component in react JS?

useNavigation is a hook and hooks can only be used inside a functional component like this. But as you are using the class component so you can navigate like this. But you have to make sure that navigation prop is available in your class component by putting console.


Video Answer


2 Answers

why you don't create your own withNavigation

import React from 'react';
import { useNavigation } from '@react-navigation/native'; // not sure package name

export const withNavigation = (Component: any) => {
  return (props: any) => {
    const navigation = useNavigation();

    return <Component navigation={navigation} {...props} />;
  };
};
like image 90
TheEhsanSarshar Avatar answered Oct 22 '22 08:10

TheEhsanSarshar


React Navigation Version: 5.x Sometimes you need to trigger a navigation action from places where you do not have access to the navigation prop, such as a Redux middleware. For such cases, you can dispatch navigation actions from the navigation container.

If you're looking for a way to navigate from inside a component without needing to pass the navigation prop down. Do not use this method when you have access to a navigation prop or useNavigation since it will behave differently, and many helper methods specific to screens won't be available.

You can get access to the root navigation object through a ref and pass it to the RootNavigation which we will later use to navigate.

// App.js

import { NavigationContainer } from '@react-navigation/native';
import { navigationRef } from './RootNavigation';

export default function App() {
  return (
    <NavigationContainer ref={navigationRef}>{/* ... */}</NavigationContainer>
  );
}

In the next step, we define RootNavigation, which is a simple module with functions that dispatch user-defined navigation actions.

// RootNavigation.js

import * as React from 'react';

export const navigationRef = React.createRef();

export function navigate(name, params) {
  navigationRef.current?.navigate(name, params);
}

// add other navigation functions that you need and export them Then, in any of your javascript modules, just import the RootNavigation and call functions that you exported from it. You may use this approach outside of your React components and, in fact, it works just as well when used from within them.

// any js module

import * as RootNavigation from './path/to/RootNavigation.js';

// ...

RootNavigation.navigate('ChatScreen', { userName: 'Lucy' });

Apart from navigate, you can add other navigation actions:

import { StackActions } from '@react-navigation/native';

export function push(...args) {
  navigationRef.current?.dispatch(StackActions.push(...args));
}

https://reactnavigation.org/docs/navigating-without-navigation-prop/

like image 29
Sumit Kumar Gupta Avatar answered Oct 22 '22 09:10

Sumit Kumar Gupta