我想将变量从一个页面通过我的bottomTabNavigator传递到另一页面,而不必使用stackNavigator或在我的tabNavigator中创建额外的“标签”
我尝试传递和接收变量,如下所述。
我如何尝试传递变量:
onPress={() => this.props.navigation.navigate('IndividualCardPage',
{"name":this.props.cards.name})}
我如何尝试在另一页上获取变量:
values = {
name: this.props.navigation.getParam('name','empty')
}
componentDidMount() {
console.log(this.values.name)
}
我希望导航到“ IndividualCardPage”,并能够在其中使用变量“ name”。 发生的事情是以下两件事之一: 1.单击按钮没有任何反应。 2.错误消息显示“无法找到变量:名称”。 有任何想法吗?预先感谢
答案 0 :(得分:0)
对于onPress导航,我必须使用以下代码:
onPress={() => this.props.navigation.navigate('iCardPage',
{"name":this.props.cards.name})}
我还必须创建一个堆栈导航器:
import React from 'react';
import {createStackNavigator} from "react-navigation";
import IndividualCardPage from '../Pages/IndividualCardPage';
import Cards from "../Pages/CardPage";
export default createStackNavigator({
Cards: {
screen: Cards,
navigationOptions: ({ navigation }) => ({
title: 'Selecteer een kaart',
backgroundColor: '#7E007F',
}),
},
iCardPage: {
screen: IndividualCardPage,
navigationOptions: ({ navigation }) => ({
title: 'Individuele kaart',
}),
},
},{
initialRouteName: 'Cards'
});
然后,当按如下所示按下BottomTabNavigator中的一个标签时,我还必须触发stacknavigator的Navigation事件:
const AppNavigator = createMaterialBottomTabNavigator({
Home: {
screen: Deck,
navigationOptions: ({ navigation }) => ({
title: 'Deck',
tabBarIcon: ({ tintColor }) => (<MaterialIcons name='home' size={25} color={tintColor} />)
})
},
Card: {
screen: StackNavigator,
navigationOptions: ({ navigation }) => ({
title: 'Cards',
tabBarIcon: ({ tintColor }) => (<MaterialIcons name='style' size={25} color={tintColor} />)
})
}
},{
initialRouteName: 'Home',
barStyle: {
backgroundColor: '#7E007F',
},
});
在页面中,我想将变量发送给我,我不得不像下面这样调整代码以获取变量(“参数”):
state = {
name: "empy"
};
async componentDidMount() {
this.setState((prevstate) => {
name: this.props.navigation.getParam("name","empty")
});
console.log(this.values.name);
}