在DrawerNavigator中使用屏幕选项传递道具

Ada*_*ver 4 javascript reactjs react-navigation

我在https://reactnavigation.org/docs/navigators/drawer中使用DrawerNavigator .

const MyApp = DrawerNavigator({
    Home: {
        screen: MyHomeScreen,
    },
    Notifications: {
        screen: MyNotificationsScreen,
    },
});
Run Code Online (Sandbox Code Playgroud)

我有多个屏幕使用MyNotificationsScreen具有不同道具的组件.

我该怎么做:

const MyApp = DrawerNavigator({
    Home: {
        screen: MyHomeScreen,
    },
    Notifications1: {
        screen: MyNotificationsScreen(propName=val1),
    },
    Notifications2: {
        screen: MyNotificationsScreen(propName=val2),
    },
});
Run Code Online (Sandbox Code Playgroud)

Luk*_*ams 9

在许多情况下我认为更好的方法:

screen: (props) => <MyNotificationsScreen {...props} propName={val1} />
Run Code Online (Sandbox Code Playgroud)

这将把你的导航道具放在props.navigation.state.params中.如果你希望它们出现在this.props中(这意味着你的组件没有紧密耦合到react-navigation),那么使用:

screen: (props) => <MyNotificationsScreen {...props.navigation.state.params} propName={val1} />
Run Code Online (Sandbox Code Playgroud)