Reactjs 将参数传递到后屏幕,并在react native中设置为状态
我试图将参数数据从第二个屏幕传递到第一个屏幕,但没有得到。我是个新来的本地人。也在谷歌上搜索过,但由于本地版本更改代码无法正常工作 这是第一个屏幕代码Reactjs 将参数传递到后屏幕,并在react native中设置为状态,reactjs,react-native,parameters,cross-platform,Reactjs,React Native,Parameters,Cross Platform,我试图将参数数据从第二个屏幕传递到第一个屏幕,但没有得到。我是个新来的本地人。也在谷歌上搜索过,但由于本地版本更改代码无法正常工作 这是第一个屏幕代码 import React, { Component } from "react"; import { Button, Text, View } from "react-native"; export default class viewA extends Component { constructor(props){ super(p
import React, { Component } from "react";
import { Button, Text, View } from "react-native";
export default class viewA extends Component {
constructor(props){
super(props);
}
state = { selected: 'hiii' };
onSelect = (data) =>{
this.setState(data)
}
onPress = () => {
this.props.navigation.navigate("viewB",{ onSelect: this.onSelect })
}
render() {
return (
<View style={{marginTop:100}}>
<Text>{this.state.selected}</Text>
<Button title="Next" onPress={this.onPress} />
<Text>{JSON.stringify(this.props)}</Text>
</View>
);
}
}
import React,{Component}来自“React”;
从“react native”导入{按钮、文本、视图};
导出默认类viewA扩展组件{
建造师(道具){
超级(道具);
}
状态={selected:'hiii'};
onSelect=(数据)=>{
此.setState(数据)
}
onPress=()=>{
this.props.navigation.navigate(“viewB”,{onSelect:this.onSelect})
}
render(){
返回(
{this.state.selected}
{JSON.stringify(this.props)}
);
}
}
第二屏幕代码-
import React, { Component } from "react";
import { Button, Text, View,Alert } from "react-native";
export default class viewB extends Component {
constructor(props){
super(props);
}
goBack= ()=> {
this.props.navigation.goBack();
this.props.navigation.state.params.onSelect({ 'selected':'hello'});
}
render() {
return (
<View style={{marginTop:100}}>
<Button title="back" onPress={this.goBack} />
<Text>{JSON.stringify(this.props)}</Text>
</View>
)
}
}
import React,{Component}来自“React”;
从“react native”导入{按钮、文本、视图、警报};
导出默认类viewB扩展组件{
建造师(道具){
超级(道具);
}
戈巴克=()=>{
this.props.navigation.goBack();
this.props.navigation.state.params.onSelect({'selected':'hello'});
}
render(){
返回(
{JSON.stringify(this.props)}
)
}
}
这里是我的链接可以编辑的代码
谢谢这就是我将数据传递到另一个屏幕的方式,请参见下面的代码和expo零食链接:
import React from 'react';
import { Button, View, Text } from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
class HomeScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Text>{this.props.navigation.getParam('itemId','No data passed')}</Text>
<Button
title="Go to Details"
onPress={() => {
this.props.navigation.navigate('Details', {
itemId: 86,
otherParam: 'anything you want here',
});
}}
/>
</View>
);
}
}
class DetailsScreen extends React.Component {
render() {
const { navigation } = this.props;
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
<Text>
itemId: {JSON.stringify(navigation.getParam('itemId', 'NO-ID'))}
</Text>
<Text>
otherParam:{' '}
{JSON.stringify(navigation.getParam('otherParam', 'default value'))}
</Text>
<Button
title="Go to Details... again"
onPress={() =>
navigation.push('Home', {
itemId: 'hello',
})
}
/>
</View>
);
}
}
const RootStack = createStackNavigator({
Home: HomeScreen,
Details: DetailsScreen,
});
export default createAppContainer(RootStack);
从“React”导入React;
从“react native”导入{按钮、视图、文本};
从“react navigation”导入{createAppContainer};
从“反应导航堆栈”导入{createStackNavigator};
类主屏幕扩展了React.Component{
render(){
返回(
主屏幕
{this.props.navigation.getParam('itemId','No data passed')}
{
this.props.navigation.navigate('Details'{
项目编号:86,
otherParam:“这里有你想要的任何东西”,
});
}}
/>
);
}
}
类DetailsScreen扩展了React.Component{
render(){
const{navigation}=this.props;
返回(
详细信息屏幕
itemId:{JSON.stringify(navigation.getParam('itemId','NO-ID'))}
otherParam:{'}
{JSON.stringify(navigation.getParam('otherParam','default value'))}
navigation.push('Home'{
itemId:'你好',
})
}
/>
);
}
}
const RootStack=createStackNavigator({
主页:主屏幕,
详情:详情屏幕,
});
导出默认createAppContainer(根堆栈);
世博链接:
主页是第一个屏幕,详细信息是第二个屏幕。我将itemId从详细信息传递到主页,然后再次发送hello,我在主屏幕中呈现为{this.props.navigation.getParam('itemId','No data passed')}
希望能有帮助。不要怀疑。你想通过什么考试?您可以指定吗?我想在Gauravroy将“hello”从第二屏传递到第一屏您为什么使用DroperNavigator?它是必需的还是stackNavigator应该满足这些要求?我会相应地修改代码。我不知道该用什么导航。如果我使用stacknavigator,它会解决问题吗@Gauravroys我应该分享一份新的世博会点心,在那里我向您展示如何在两个屏幕之间传递数据?