javascript - 如何使用 Tab Navigator NOT stack navigator 将参数传递给路由

标签 javascript node.js react-native react-navigation expo

我正在使用 React 导航库,特别是 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 文件,我想将其传递给 alertsScreen.js 中的 alerts 路由

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],
            });

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

最佳答案

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

并不重要

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

this.props.navigation.navigate('jsonWatchList', { jsonWatchList:[这里有很多数据], });

在你的另一个屏幕上,让他们像:

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

关于javascript - 如何使用 Tab Navigator NOT stack navigator 将参数传递给路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55471298/

相关文章:

node.js - 找到一种使用 Passport-local-mongoose 更新用户 Passport 的方法

javascript - 运行 Selenium-webdriver 时 ECONNREFUSED 连接

android - 如何触发react-native应用程序唤醒? (用于报警)

javascript - React Native Navigation 将所有屏幕包裹在一个 View 中

javascript - 将对象数组转换为包含对象数组的对象(复杂)

javascript - React.js 中的数据存储在哪里?

javascript - 这些 Jquery、ExtJs 框架如何覆盖原生 Javascript

Azure 网站上的 Node.js 404 错误

javascript - onkeydown 函数上的 setInterval

javascript - 动态改变AngularJS中静态段落的颜色