React native componentDidUpdate在执行网络请求时给出RCTDeviceEventEmitter错误
我的应用程序有一个组件,用户每次刷卡都会更新该组件。React native componentDidUpdate在执行网络请求时给出RCTDeviceEventEmitter错误,react-native,React Native,我的应用程序有一个组件,用户每次刷卡都会更新该组件。 该组件每次都会获得新道具,其值也会相应更新。 收到新数据后,我创建了一个facebook graph api获取请求,以获取用户的个人资料图像。 为了使它看起来更好,我添加了一个默认图像,直到fetch请求完成。我在组件willreceiveprops中将图像重置为默认值 我得到的错误是“调用函数:RCTDeviceEventEmitter:emit时出错” 只有在我没有进行远程调试(使用react native的chrome调试器)时才会发
该组件每次都会获得新道具,其值也会相应更新。
收到新数据后,我创建了一个facebook graph api获取请求,以获取用户的个人资料图像。
为了使它看起来更好,我添加了一个默认图像,直到fetch请求完成。我在
组件willreceiveprops
中将图像重置为默认值我得到的错误是“调用函数:RCTDeviceEventEmitter:emit时出错”
只有在我没有进行远程调试(使用react native的chrome调试器)时才会发生这种情况。
以下是相关代码:
componentWillReceiveProps(nextProps){
var temp;
temp=this.state.profileInfo;
temp.image='http://s3.amazonaws.com/cdn.roosterteeth.com/default/tb/user_profile_female.jpg';
this.setState({
profileInfo:temp,
});
},
componentDidUpdate(prevProps, prevState){
if(prevProps!=this.props){
console.log("Updated!!!");
var api = 'https://graph.facebook.com/' + this.props.owner_id +
'/picture?type=normal&access_token=' + this.props.credentials.token;
fetch(api)
.then((response) =>{
var temp;
temp=this.state.profileInfo;
temp.image=response.url;
this.setState({
profileInfo:temp,
});
})
.done();
}
},
componentDidMount(){
var api = 'https://graph.facebook.com/' + this.props.owner_id +
'/picture?type=normal&access_token=' + this.props.credentials.token;
fetch(api)
.then((response) =>{
var temp;
temp=this.state.profileInfo;
temp.image=response.url;
this.setState({
profileInfo:temp,
});
})
.done();
},
有人知道我如何解决这个错误吗?为什么只有在未调试时才使用它?请查看以下内容:。有人用尽可能少的代码打开了相同的问题,因此这可能意味着问题不在React组件上
无论如何,您可以删除componentDidUpdate方法,如下所示:
componentWillReceiveProps(nextProps){
var temp;
temp=this.state.profileInfo;
temp.image='http://s3.amazonaws.com/cdn.roosterteeth.com/default/tb/user_profile_female.jpg';
this.setState({
profileInfo:temp,
}, () => {
if(nextProps!=this.props) {
console.log("Updated!!!");
var api = 'https://graph.facebook.com/' + nextProps.owner_id +
'/picture?type=normal&access_token=' + nextProps.credentials.token;
fetch(api)
.then((response) => {
var temp;
temp = this.state.profileInfo;
temp.image = response.url;
this.setState({
profileInfo: temp,
});
})
.done();
}
});
},
componentDidMount(){
var api = 'https://graph.facebook.com/' + this.props.owner_id +
'/picture?type=normal&access_token=' + this.props.credentials.token;
fetch(api)
.then((response) =>{
var temp;
temp=this.state.profileInfo;
temp.image=response.url;
this.setState({
profileInfo:temp,
});
})
.done();
},
错误消失了,但不起作用<代码>如果(nextrops!=this.props)始终为false,我从不更新图片。当我看到保存当前和以前道具的日志时,它们是不同的。你知道为什么吗?每次收到新道具时都会调用componentWillReceiveProps<代码>下一步=这个.props实际上应该总是返回true(不确定为什么会变为false)。因此,要检查道具是否发生了变化,我们应该逐个检查道具的属性(比如:if(nextrops.owner\u id!=this.props.owner\u id)),或者进行深入的比较。在您的情况下,如果您希望每次都重新获取结果,您可以删除
if(nextrops!=this.props)
。我删除了该条件,现在我在不处于调试模式时收到相同的错误。这意味着该问题可能与您的特定组件无关。