我对原生反应非常陌生。我在我的react原生应用程序中使用react导航。我将一些道具从一个屏幕传递到另一个屏幕,并且我需要在componentDidMount
生命周期方法中尝试执行的fetch中使用其中一个道具。对于我尝试过的所有方法,它都会发送"type“键的值,但不会发送"location”键的值(参见下面的代码)。有没有人能帮我解决我遗漏了什么或做错了什么?我已经尝试了几种方法来通过这个道具,但是都没有起作用。
componentDidMount() {
const { params } = this.props.navigation.state;
var data = {
type: 'r',
location: params.location
}
return fetch('http://myapisite', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
}
)
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
dataSource: responseJson,
}, function() {
// do something with new state
});
})
.catch((error) => {
console.error(error);
});
}
发布于 2018-02-02 05:08:55
我能够解决这个问题。我不是百分之百确定为什么我的东西不能工作,但当我把它改为以下内容时,它就像预期的那样工作了:
const {state} = this.props.navigation;
var data = {
type: 'restaurant',
location: state.params.location
}
return fetch('http://mylink', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
}
)
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
dataSource: responseJson,
}, function() {
// do something with new state
});
})
.catch((error) => {
console.error(error);
});
https://stackoverflow.com/questions/48457834
复制