React native 如何将API响应存储在状态中,并将此响应值作为参数在react native中传递给另一个屏幕
我是个新来的本地人。我已经创建了一个屏幕。我从API那里得到了回应。但现在我想将该响应存储在state中。我想通过导航参数将该值发送到另一个屏幕。 我的回答是这样的->React native 如何将API响应存储在状态中,并将此响应值作为参数在react native中传递给另一个屏幕,react-native,React Native,我是个新来的本地人。我已经创建了一个屏幕。我从API那里得到了回应。但现在我想将该响应存储在state中。我想通过导航参数将该值发送到另一个屏幕。 我的回答是这样的-> Array [ Object { "phpid": 10, }, ] 这是我的密码 constructor(props) { super(props); this.state={ }; } fetch('https://xuz.tech/Android_API_
Array [
Object {
"phpid": 10,
},
]
这是我的密码
constructor(props) {
super(props);
this.state={
};
}
fetch('https://xuz.tech/Android_API_CI/uploaddata/t_details?query=', {
method: 'POST',
headers: {'Accept': 'application/json, text/plain, */*', "Content-Type": "application/json" },
body: JSON.stringify([{"==some values=="}])
})
.then((returnValue) => returnValue.json())
.then(function(response) {
console.log(response)
return response.json();
render(){
return (
<View style={{flex: 1}}>
color="black" onPress={() => this.props.navigation.navigate("FormItems",{i want to send value to formitems})} />
</View>
)}
构造函数(道具){
超级(道具);
这个州={
};
}
取('https://xuz.tech/Android_API_CI/uploaddata/t_details?query=', {
方法:“POST”,
标题:{'Accept':'application/json,text/plain,*/*',“Content Type”:“application/json”},
正文:JSON.stringify([{“==某些值==”}])
})
.then((returnValue)=>returnValue.json()
.然后(功能(响应){
console.log(响应)
返回response.json();
render(){
返回(
color=“black”onPress={()=>this.props.navigation.navigate(“FormItems”,{i想要将值发送到FormItems}}/>
)}
收到响应后设置您的状态,然后在导航时将您的状态用作参数。解决提取后:
this.setState({ response: response.json() });
将参数发送到另一个屏幕相当简单,只需将对象作为第二个参数传递即可进行导航
this.props.navigation.navigate('FormItems', {
form: this.state.response,
});
然后,接收组件将需要读取这些参数:
class DetailsScreen extends React.Component {
render() {
const { navigation } = this.props;
return (
<Text>{JSON.stringify(navigation.getParam('form', 'some default'))}</Text>
}
}
class DetailsScreen扩展了React.Component{
render(){
const{navigation}=this.props;
返回(
{JSON.stringify(navigation.getParam('form','somedefault'))}
}
}
有关如何在react navigation v4中使用参数的完整说明,请参见此处:在收到响应后设置状态,然后在导航时将状态用作参数。解决提取后:
this.setState({ response: response.json() });
将参数发送到另一个屏幕相当简单,只需将对象作为第二个参数传递即可进行导航
this.props.navigation.navigate('FormItems', {
form: this.state.response,
});
然后,接收组件将需要读取这些参数:
class DetailsScreen extends React.Component {
render() {
const { navigation } = this.props;
return (
<Text>{JSON.stringify(navigation.getParam('form', 'some default'))}</Text>
}
}
class DetailsScreen扩展了React.Component{
render(){
const{navigation}=this.props;
返回(
{JSON.stringify(navigation.getParam('form','somedefault'))}
}
}
有关如何在react navigation v4中使用参数的完整说明,请参见此处:像这样使用它。首先初始化状态,当您从api获取数据时,将数据设置为状态,当按下按钮时,将数据传递到params中的新屏幕
import React, { Component } from 'react';
import { Text, View } from 'react-native';
export default class Example extends Component {
state = {
data: [], // initialize empty state
};
componentWillMount() {
this.requestData();
}
requestData = () =>{
fetch('https://xuz.tech/Android_API_CI/uploaddata/t_details?query=', {
method: 'POST',
headers: {'Accept': 'application/json, text/plain, */*', "Content-Type": "application/json" },
body: JSON.stringify([{"==some values=="}])
})
.then((returnValue) => returnValue.json())
.then(function(response) {
this.setState({
data:response //set data in state here
})
})
}
render() {
return (
<View style={{ flex: 1 }}>
<Button
color="black"
onPress={() =>
this.props.navigation.navigate('FormItems', {
data: this.state.data, // pass data to second screen
})
}
/>
</View>
);
}
}
import React,{Component}来自'React';
从“react native”导入{Text,View};
导出默认类示例扩展组件{
状态={
数据:[],//初始化空状态
};
组件willmount(){
这个.requestData();
}
请求数据=()=>{
取('https://xuz.tech/Android_API_CI/uploaddata/t_details?query=', {
方法:“POST”,
标题:{'Accept':'application/json,text/plain,*/*',“Content Type”:“application/json”},
正文:JSON.stringify([{“==某些值==”}])
})
.then((returnValue)=>returnValue.json()
.然后(功能(响应){
这是我的国家({
数据:响应//将数据设置为此处的状态
})
})
}
render(){
返回(
this.props.navigation.navigate('FormItems'{
data:this.state.data,//将数据传递到第二个屏幕
})
}
/>
);
}
}
这样使用它。首先初始化状态,当您从api获取数据时,将数据设置为状态,当按下按钮时,将数据传递到参数中的新屏幕
import React, { Component } from 'react';
import { Text, View } from 'react-native';
export default class Example extends Component {
state = {
data: [], // initialize empty state
};
componentWillMount() {
this.requestData();
}
requestData = () =>{
fetch('https://xuz.tech/Android_API_CI/uploaddata/t_details?query=', {
method: 'POST',
headers: {'Accept': 'application/json, text/plain, */*', "Content-Type": "application/json" },
body: JSON.stringify([{"==some values=="}])
})
.then((returnValue) => returnValue.json())
.then(function(response) {
this.setState({
data:response //set data in state here
})
})
}
render() {
return (
<View style={{ flex: 1 }}>
<Button
color="black"
onPress={() =>
this.props.navigation.navigate('FormItems', {
data: this.state.data, // pass data to second screen
})
}
/>
</View>
);
}
}
import React,{Component}来自'React';
从“react native”导入{Text,View};
导出默认类示例扩展组件{
状态={
数据:[],//初始化空状态
};
组件willmount(){
这个.requestData();
}
请求数据=()=>{
取('https://xuz.tech/Android_API_CI/uploaddata/t_details?query=', {
方法:“POST”,
标题:{'Accept':'application/json,text/plain,*/*',“Content Type”:“application/json”},
正文:JSON.stringify([{“==某些值==”}])
})
.then((returnValue)=>returnValue.json()
.然后(功能(响应){
这是我的国家({
数据:响应//将数据设置为此处的状态
})
})
}
render(){
返回(
this.props.navigation.navigate('FormItems'{
data:this.state.data,//将数据传递到第二个屏幕
})
}
/>
);
}
}
您是否使用react导航?如果是,哪个版本?是4.4.3 i使用raect导航版本i具有发送值,但这是唯一的我不存储从响应到状态并在之前使用它您是否使用react导航?如果是,哪个版本?是4.4.3 i使用raect导航版本i具有发送值,但这是唯一的我不从响应存储nse之前声明并使用它,但如何将响应存储在state@SohilShaikh更新了我的回答我应该放什么而不是一些defautnavigation。如果getParam找不到您要查找的参数,它将返回null。如果是这种情况,请将“某些默认值”替换为您可以使用的数据,否则您可以在继续之前执行null检查(如果组件运行绝对需要数据)但是如何将响应存储在state@SohilShaikh更新了我的回答我应该放什么而不是一些defautnavigation。如果getParam找不到您要查找的参数,它将返回null。如果是这种情况,请将“某些默认值”替换为您可以使用的数据,否则您可以在继续之前执行null检查(如果您的组件运行绝对需要数据)与Phobos已经发布的答案相同,但在我发布时没有发布