我正在使用本机生成一个Android应用程序。我想通过TabNavigator传递params,但是 this.props.navigation.state.params未定义。目前我正在使用:
"react-native": "0.51.0",
"react-navigation": "^1.0.0-beta.22"
示例代码(试图仅保留相关内容):
这是我的index.js:
const RootNavigator = TabNavigator({
Home: { screen: App },
Design: { screen: ImageDesign }
}
这是主屏幕:
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = { ... }
}
static navigationOptions = {
tabBarVisible: false
};
render() {
const { navigate } = this.props.navigation;
return (
<View style={styles.container}>
<Button title="design image"
onPress={ () => navigate('Design',{name: 'Jane'}) }/>
</View>
);
}
}
这是设计界面:
export default class ImageDesign extends React.Component {
constructor(props) {
super(props);
this.state = { ... }
}
static navigationOptions = {
tabBarVisible: false
};
render() {
const { params } = this.props.navigation.state;
return (
<View>
<Text>{ params.name }</Text>
</View>
);
}
}
但不幸的是,params是未定义的,因此未定义的不是对象&#39;错误确认
我也尝试过:
<Button title="design image"
onPress={ () => navigate('Design', {}, {type: "Navigate", routName:"ImageDesign", params: {name: 'Jane'}}) }/>
但是我不确定那样的方式
提前谢谢!
答案 0 :(得分:0)
试试this.props.name
。参数/道具应作为道具发送。您不应该像这样访问导航状态。