3

I want to pass something by screenProps in React-navigation v5.x.x. I am one of the newcomers in react-native. Can anyone help me?

Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
Roa
  • 437
  • 4
  • 14
  • 1
    For your next question, you may want to tag your question with react-navigation so more people can see it. – satya164 Feb 12 '20 at 19:28

3 Answers3

7

There's no screenProps in React Navigation 5. You can use React's Context feature instead to pass data down the tree without an extra API.

https://reactnavigation.org/docs/upgrading-from-4.x#global-props-with-screenprops

Cristiano Mendonça
  • 1,220
  • 1
  • 10
  • 21
satya164
  • 9,464
  • 2
  • 31
  • 42
  • updated link https://reactnavigation.org/docs/5.x/upgrading-from-4.x/#global-props-with-screenprops – howard Aug 21 '23 at 05:20
0

in my case I am passing my data like :

props.navigation.navigate('toScreen', {
    resumeDetail: data,
})

and you can access it like :

detail = this.props.navigation.state.params.resumeDetail;
Tushar Pandey
  • 4,557
  • 4
  • 33
  • 50
  • 1
    Thanks my friend , but i didn't mean about this process. I wanted to know how to do it in by screenProps – Roa Feb 12 '20 at 11:01
0

https://reactnavigation.org/docs/screen/#children

Render callback to return React Element to use for the screen:

<Stack.Screen name="Profile">
 {(props) => <ProfileScreen {...props} />}
</Stack.Screen>

You can use this approach instead of the component prop if you need to pass additional props. Though we recommend using React context for passing data instead.

Note: By default, React Navigation applies optimizations to screen components to prevent unnecessary renders. Using a render callback removes those optimizations. So if you use a render callback, you'll need to ensure that you use React.memo or React.PureComponent for your screen components to avoid performance issues.

This is what I use at the moment:

// ...

export const ScanPage = React.memo(ScanComponent);
function useScreenWithProps(
  PageComponent: React.FC<Props>,
  props: Props
) {
  // Take note of the double arrow,
  // the value useMemo returns is a function that returns a component.
  return useMemo(
    () => (navigationProps: NavigationProps) => (
      <PageComponent {...navigationProps} {...props} />
    ),
    [PageComponent, props]
  );
}

const Stack = createStackNavigator();

const Navigator: React.FC<Props> = (props) => {
  const scan = useScreenWithProps(ScanPage, props);
  const activate = useScreenWithProps(ActivatePage, props);
  const calibrate = useScreenWithProps(CalibratePage, props);
  const success = useScreenWithProps(SuccessPage, props);
  const error = useScreenWithProps(ErrorPage, props);

  return (
    <Stack.Navigator>
      <Stack.Screen name="Scan">{scan}</Stack.Screen>
      <Stack.Screen name="Activate">{activate}</Stack.Screen>
      <Stack.Screen name="Calibrate">{calibrate}</Stack.Screen>
      <Stack.Screen name="Success">{success}</Stack.Screen>
      <Stack.Screen name="Error">{error}</Stack.Screen>
    </Stack.Navigator>
  );
};
ThaJay
  • 1,758
  • 1
  • 19
  • 30