1

How can I trigger navigation from actions file when using react-navigation v5 and redux inside my react-native app? Is there any preferable way on how to do this? When result is fulfilled I want to trigger the navigation to the next screen, but I can't use useNavigation(). Thanks.

2 Answers 2

5

Create a ref to the NavigationContainer

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

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

Create a module RootNavigation.js to hold the ref

import * as React from 'react';

export const navigationRef = React.createRef();

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

Now trigger navigation from any place using below:

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

//...

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

Can I find somewhere version with typescript?
I was also looking for the same solution. The answer helped me. Thank you (y)
0

Would this question help you? It seems that it was made by someone trying to accomplish a similar scenario to yours

React-Navigation: navigate from actions file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.