如何在React Navigation v5中将导航道具传递给子组件

时间:2020-08-03 11:25:39

标签: javascript react-native react-navigation react-navigation-v5

如何在react native中将导航道具传递给子组件,我正在使用React Navigation v5,因为我想在子组件中使用navigation.navigate('settings')。

1 个答案:

答案 0 :(得分:0)

您可以在子组件中使用useNavigation

示例(根据文档改编):

import React from 'react';
import { Button } from 'react-native';
import { useNavigation } from '@react-navigation/native';

function MyBackButton() {
  const navigation = useNavigation();

  return (
    <Button
      title="Go to Settings"
      onPress={() => {
        navigation.navigate('settings');
      }}
    />
  );
}