我知道有许多相似的主题,但由于我很难理解答案,我想我可以尝试使用自己的代码,看看能不能理解答案。
我只是简单地设置它来测试它。我有一个在启动应用程序时打开的索引文件。在索引中,我在this.state中有testValue:
更新
在SignIn:
constructor(props){
super(props);
this.state={
credentials: {
username: "",
password:"",
}
}
this.navigate = this.props.navigation.navigate;
{...}
this.navigate("main", {
credentials: this.state.username,
});
In main:
constructor(props) {
super(props);
this.params = this.props.navigation.state.params;
this.navigate = this.props.navigation.navigate;
{...}
render() {
console.log(this.params.username);
答案 0 :(得分:1)
这实际上会记录testValue。您所要做的就是将testValue状态作为TestIndex组件中的prop传递。像这样:
<强> Index.jsx 强>
export default class Index {
constructor(props) {
super(props);
this.state = {
testValue: 'hello'
}
}
render() {
return <TestIndex testValue={this.state.testValue} />
}
}
<强> TestIndex.jsx 强>
export default class TestIndex extends React.Component {
index() {
this.props.navigation.navigate("index")
}
handleClickMain = () => {
this.props.navigation.navigate("index");
}
render() {
return (
<View style={styles.container}>
<TouchableOpacity style={styles.btn} onPress={() => {
console.log(this.props.testValue) //here's where I want it to log testValue from the index file
this.handleClickIndex()
}
}>
</TouchableOpacity>
</View>
);
}
}