Javascript 如何在react native中将数据从一个屏幕发送到另一个屏幕?
我是一个新的反应本机我如何才能发送数据从一个屏幕到另一个屏幕使用道具在安卓不是ios我的代码如下 Home.jsJavascript 如何在react native中将数据从一个屏幕发送到另一个屏幕?,javascript,react-native-android,react-navigation,Javascript,React Native Android,React Navigation,我是一个新的反应本机我如何才能发送数据从一个屏幕到另一个屏幕使用道具在安卓不是ios我的代码如下 Home.js class Home extends React.Component { constructor(props) { super(props); this.state = { qwerty:{ data:[], }, }; } goPressed(navigate){ navigate("Prod
class Home extends React.Component {
constructor(props) {
super(props);
this.state = {
qwerty:{
data:[],
},
};
}
goPressed(navigate){
navigate("Product");
}
render() {
const { navigate } = this.props.navigation;
contents = this.state.qwerty.data.map((item) => {
return (
<View key={item.p1.id}>
<View>
<Text>{item.p1.content}</Text>
</View>
<View>
<TouchableHighlight onPress={() => this.goPressed(navigate)}>
<Text>
Go
</Text>
</TouchableHighlight>
</View>
</View>
);
});
return (
<ScrollView style={styles.container}>
{contents}
</ScrollView>
);
}
}
export default Home;
class Home扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
qwerty:{
数据:[],
},
};
}
按下按钮(导航){
导航(“产品”);
}
render(){
const{navigate}=this.props.navigation;
contents=this.state.qwerty.data.map((项)=>{
返回(
{item.p1.content}
this.goPressed(导航)}>
去
);
});
返回(
{contents}
);
}
}
导出默认主页;
这是我的home.js,我想把数据,即{item.p1.content}传递到另一个屏幕,即product.js,那么我该怎么做呢
Product.js
export default class Products extends Component {
static navigationOptions = {
title: "Products",
};
render() {
return (
<View style={{ flex: 1 }}>
<Text>{item.p1.content}</Text>
</View>
);
}
}
导出默认类产品扩展组件{
静态导航选项={
标题:“产品”,
};
render(){
返回(
{item.p1.content}
);
}
}
一种方法是将存储在“qwerty”中的日期作为道具传递给下一个场景
在Home.js中,您可以修改您的goPressed方法,使其类似于
goPressed(navigate){
navigate("Product", {passedData: this.state.qwerty.item.p1.content});
}
然后在Product.js中,您需要修改代码以
render() {
return (
<View style={{ flex: 1 }}>
<Text>{this.props.passedData}</Text>
</View>
);
}
render(){
返回(
{this.props.passedData}
);
}
将数据发送到其他屏幕
从其他屏幕接收数据
你好使用此方法进行导航,但在控制台上选中时,数据不会传递到
产品
&显示未定义
。是否需要在Product.js
中导入
某些内容或设置状态
。当您将Home.js更改为:导航(“Product”{passedData:this.state.qwerty})时会得到什么;
this.props.navigation.navigate('Your Screen Name' , { YourParamsName: "Foo"});
this.props.navigation.state.params.YourParamsName