我正在使用 react native 制作一个 android 应用程序。我试图通过 TabNavigator 传递参数,但是this.props.navigation.state.params 是未定义的。目前我正在使用:
"react-native": "0.51.0",
"react-navigation": "^1.0.0-beta.22"
示例代码(尝试仅保留相关(:
这是我的索引.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>
);
}
}
但不幸的是,参数是未定义的,因此"未定义不是对象"错误
我也试过:
<Button title="design image"
onPress={ () => navigate('Design', {}, {type: "Navigate", routName:"ImageDesign", params: {name: 'Jane'}}) }/>
但我不确定这样工作
提前谢谢你!
试试this.props.name
.参数/道具应作为道具发送。您不需要像那样访问导航状态。