如何使用Tab Navigator NOT堆栈导航器将参数传递给路径

问题描述 投票:1回答:1

我使用反应导航库,特别是createBottomTabNavigator

https://reactnavigation.org/docs/en/params.html上的文档解释了如何使用堆栈导航器在路径之间传递参数但是我使用了一个标签导航器,我无法找到任何跳出来向我解释如何在标签导航设置中执行此操作

我在App.js中的标签导航器是

const BottomTabMenu = createBottomTabNavigator (
  {
    WatchList: { screen: WatchListScreen },
    Alerts: { screen: AlertsScreen },
    Analytics: { screen: AnalyticsScreen },
    Settings: { screen: SettingsScreen },
  },
  {
    initialRouteName: 'WatchList',
    defaultNavigationOptions: ({ navigation }) => ({
      tabBarIcon: ({ focused, horizontal, tintColor }) => {
        const { routeName } = navigation.state;
        let IconComponent = Ionicons;
        let iconName;
        if (routeName === 'WatchList') { iconName = 'md-list'; }  
        if (routeName === 'Alerts') { iconName = 'md-alert'; }  
        if (routeName === 'Analytics') { iconName = 'md-analytics'; }  
        if (routeName === 'Settings') { iconName = 'md-settings'; }

        return <IconComponent name={iconName} size={25} color={tintColor} />;
      },
    }),
    tabBarOptions: {
      activeTintColor: 'tomato',
      inactiveTintColor: 'gray',
    },
  }
);
const AppContainer = createAppContainer(BottomTabMenu);

watchList路由包含一个名为symbols的json文件,我希望将其传递给警报Screen.js中的警报路由

export default class WatchListScreen extends React.Component {

    constructor(props) {
        super(props)
        this.state = {
            symbols: [],
        }
    }  
LOTS OF OTHER STUFF

ALERTSSCREEN.JS

export default class AlertsScreen extends React.Component {

    render() {
        //according to the docs, this is how to receive in the props
        const { navigation } = this.props;
        const jsonWatchList = navigation.getParam('jsonWatchList', '[]');

        return (
        <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
            <Text>blah blah blah</Text>
        </View>
        );
    }
}

根据文档,我需要传递数据的行将是这样的

this.props.navigation.navigate('Alerts', {
              jsonWatchList: [lots of data here],
            });

唯一的问题是我不知道应该去哪里。

javascript node.js react-native react-navigation expo
1个回答
1
投票

您的链接(https://reactnavigation.org/docs/en/params.html)中有详细记录。它解释了如何在路线之间传递参数。如果你使用的是stackNavigatorcreateBottomTabNavigator并不重要

像你想要的那样传递你的参数:

this.props.navigation.navigate('jsonWatchList', { jsonWatchList: [lots of data here], });

在你的其他屏幕上,让他们像:

const data = this.props.navigation.getParam('jsonWatchList');

© www.soinside.com 2019 - 2024. All rights reserved.