我正在使用 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 ) 中有详细记录。
它解释了如何在路由之间传递参数。使用 stackNavigator
或 createBottomTabNavigator
像你想的那样传递你的参数:
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/