我将数据发送到Page1组件。然后我从那里到达
this.props.navigation.state.params.datas.map
当应用程序处于调试模式时,一切正常。但是当我停止调试时,它给了我那个错误的提示
我这样发送:
controlAllMeth() {
Promise.all([this.callFetch(), this.startAnim()])
.then(([fetchResponse, animResponse]) => {
this.setState({PersonalInfo: fetchResponse[0]});
if(this.state.username != '')
{
this.props.navigation.navigate('Page1',{datas: this.state.PersonalInfo});
}
else
{
this.props.navigation.navigate('login');
}
})
.catch(err => {
});
}
我这样看:
constructor(props) {
super(props);
this.props.navigation.state.params.datas.map(color => {
if(color.Renk == 'MAVI'){
Blue.push("MAVI");
}
else if(color.Renk == 'SARI')
{
Yellow.push("SARI")
}
})
}
我在Promise.all
中使用controlAllMeth()
等待网络服务完成。在调试模式下可以,但是如果启用调试,则可以正常工作。
我在哪里称呼网络服务
callFetch() {
let collection={}
collection.username = this.state.username,
collection.password = this.state.password
var x = [] ;
fetch('url', {
method: 'POST',
headers: new Headers({
Accept: 'application/json',
'Content-Type': 'application/json', // <-- Specifying the Content-Type
}),
body: JSON.stringify(collection) // <-- Post parameters
})
.then((response) => response.text())
.then(leaders => {
const str = leaders.substring(6);
const str4 = str.substring(0,str.length - 2);
const str5 = str4.replace(/\\/g, '');
const str3 = JSON.parse(str5);
console.log("callfetch", str3);
// this.savedata(collection.username,collection.password)
x.push(str3);
});
return x;
}
答案 0 :(得分:1)
当您尝试访问controllAllMeth
之前,您的问题出在state
函数中,因此该值将是undefined
。
在setState
中使用回调,或者在使用值之前定义值。最简单的方法是定义值:
controlAllMeth() {
Promise.all([this.callFetch(), this.startAnim()])
.then(([fetchResponse, animResponse]) => {
const datas = fetchResponse[0] // define the value
this.setState({PersonalInfo: datas});
if(this.state.username != '')
{
this.props.navigation.navigate('Page1',{datas: datas}); // use the value you defined rather than the value from state
}
else
{
this.props.navigation.navigate('login');
}
})
.catch(err => {
});
}
controlAllMeth () {
Promise.all([this.callFetch(), this.startAnim()])
.then(([fetchResponse, animResponse]) => {
this.setState({ PersonalInfo: fetchResponse[0] }, () => { // use a callback to guarantee state has been updated
if (this.state.username != '')
{
this.props.navigation.navigate('Page1', { datas: this.state.PersonalInfo });
}
else
{
this.props.navigation.navigate('login');
}
});
})
.catch(err => {
});
}
关于setState