简体   繁体   English

将 props 中的导航传递给 React Native 和 React Navigation 中的菜单元素

[英]Pass navigation in props to menu element in React Native and React Navigation

I'm adding a menu to every screen navigation header, so created a menu function that needs the navigation object to navigate to different screens based on the menu item tapped.我正在向每个屏幕导航 header 添加一个菜单,因此创建了一个菜单 function 需要navigation object 根据所点击的菜单项导航到不同的屏幕。 But the navigation.navigate from props is undefined .但是来自propsnavigation.navigateundefined

export default function GeneralMenu(props) {
    const [showMenu, setShowMenu] = useState(false);

  return (
    <View style={{}}>
      <Menu
        visible={showMenu}
        onDismiss={() => setShowMenu(false)}
        anchor={
          <TouchableOpacity onPress={() => setShowMenu(true)}>
            <MaterialCommunityIcons
              name="menu"
              size={30}
              style={{ color: 'white' }}
            />
          </TouchableOpacity>
        }>
        <Menu.Item onPress={() => { console.log(props.navigation)}} title="Screen1" />
        <Menu.Item onPress={() => {props.navigation.navigate('Screen1', {})}} title="Screen1" />
      </Menu>
    </View>
  );

Then I use it like this:然后我像这样使用它:

return (
    <Provider>    
        <NavigationContainer>
          
          <Stack.Navigator>
            <Stack.Screen
              name="Home"
              component={HomeComponent}
              options={{ title: 'Home',
                headerTitleAlign: 'center',
                headerTintColor: '#fff',
                headerStyle: {
                  backgroundColor: 'red'
                },
                headerRight: (navigation) => <GeneralMenu navigation={navigation} />,
              }}
            />
            ... 
            ...
        </NavigationContainer>
    </Provider>
);

The console.log() when first menu item is tapped shows:点击第一个菜单项时的console.log()显示:

Object {
  "canGoBack": false,
  "tintColor": "#fff",
}

As you see there is no navigate property.如您所见,没有导航属性。 Please, any idea what I'm doing wrong?请问,知道我做错了什么吗?

To get navigation.navigate() you would wanna use the function syntax of options , which receives an object that contains navigation , like so:要获得navigation.navigate()你会想要使用options的 function 语法,它接收一个包含navigation的 object ,像这样:

<Stack.Screen
  name="Home"
  component={HomeComponent}
  options={({ navigation }) => ({
    title: "Home",
    headerTitleAlign: "center",
    headerTintColor: "#fff",
    headerStyle: {
      backgroundColor: "red",
    },
    headerRight: () => <GeneralMenu navigation={navigation} />,
  })}
/>

its happen because u shadowing name props , it should be它的发生是因为你隐藏了名字props ,它应该是

<Menu.Item onPress={() => { console.log(props.navigation)}} title="Screen1" />

<Menu.Item onPress={() => {props.navigation.navigate('Screen1', {})}} title="Screen1" />

I'm not sure why you are passing navigation through props.我不确定你为什么要通过道具传递导航。 But for react-navigation to navigate to other screens (even from the components that are not screen themselves), you don't need to pass them through props.但是对于 react-navigation 导航到其他屏幕(甚至从不是屏幕本身的组件),您不需要通过道具传递它们。

You can basically access navigation object itself by using useNavigation hook like this:您基本上可以使用这样的 useNavigation 挂钩访问导航 object 本身:

const navigation = useNavigation();

and then wherever you need to use it, you can use it normally like:然后无论你需要在哪里使用它,你都可以像这样正常使用它:

navigation.navigate(Screen name goes here);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM