Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 使用componentWillMount和componentDidMount对本机设置状态(…;)警告作出反应_React Native - Fatal编程技术网

React native 使用componentWillMount和componentDidMount对本机设置状态(…;)警告作出反应

React native 使用componentWillMount和componentDidMount对本机设置状态(…;)警告作出反应,react-native,React Native,我从react native开始,在我的项目中,我已经到了一切正常的地步,但有一个警告: 警告:设置状态(…):只能更新已安装或正在安装的组件。 因此,我查看了一些QA,尝试了一些解决方案(从componentWillMount和componentDidMount更改setState()调用),但是。。。警告总是存在的 以下是代码的一部分: REQUEST_URL = 'http://url/users.php'; (...) module.exports = React.createCla

我从react native开始,在我的项目中,我已经到了一切正常的地步,但有一个警告:

警告:设置状态(…):只能更新已安装或正在安装的组件。

因此,我查看了一些QA,尝试了一些解决方案(从
componentWillMount
componentDidMount
更改
setState()
调用),但是。。。警告总是存在的

以下是代码的一部分:

REQUEST_URL = 'http://url/users.php';

(...)

module.exports = React.createClass({
    getInitialState: function() {
        return {
            uid: null,
            bid: null,
            username: null,
        }
    },

    componentDidMount: function() {
        this.fetchData();
    },
    fetchData: function() {
        fetch(REQUEST_URL)
            .then( (response) => response.json() )
            .then( (json) => {
                console.log('setState called');
                this.setState({
                    uid: json.users[0].user_id,
                    bid: json.users[0].building_id,
                    username: json.users[0].username
                });
            })
            .done();
    },
    render: function() {
        if (!this.state.uid) { //user is not defined
            console.log('not rendered');
            return <Text>chargement...</Text>
        }
        // else
        console.log('rendered');
        var userId = this.state.uid;
        var buildingId = this.state.bid;
        var username = this.state.username;

        return (
            <View style={styles.content}>
                <Text style={styles.label}>User Id</Text>
                <Text>{userId}</Text>

                <Text style={styles.label}>Building Id</Text>
                <Text>{buildingId}</Text>

                <Text style={styles.label}>Username</Text>
                <Text>{username}</Text>
            </View>
        )
    },
});
请求http://url/users.php'; (...) module.exports=React.createClass({ getInitialState:函数(){ 返回{ uid:null, 出价:空, 用户名:null, } }, componentDidMount:function(){ 这是fetchData(); }, fetchData:function(){ 获取(请求URL) .then((response)=>response.json()) 。然后((json)=>{ log('setState called'); 这是我的国家({ uid:json.users[0]。用户id, bid:json.users[0]。正在生成\u id, 用户名:json.users[0]。用户名 }); }) .完成(); }, render:function(){ 如果(!this.state.uid){//未定义用户 console.log(“未呈现”); 退货收费。。。 } //否则 console.log('rendered'); var userId=this.state.uid; var buildingId=this.state.bid; var username=this.state.username; 返回( 用户Id {userId} 建筑物Id {buildingId} 用户名 {username} ) }, }); php返回一个json内容类型

有什么线索吗


Thanx.

问题可能是react在一次渲染中多次重新装载某些组件(认为这与初始值的表示有关,在这里找不到问题),因此您的
状态将设置为未装载的组件

如果在组件卸载时可以清除的解耦超时中设置
状态
,则可以避免在卸载的组件上设置状态

componentDidMount() {
  this.mounted = true;
  // this.fetchTimeout = setTimeout(()=>{
    this.fetchData();
  // });
},
componentWillUnmount() {
  // clearTimeouts(this.fetchTimeout);
  this.mounted = false;
},
fetchData() {
    fetch(REQUEST_URL)
        .then( (response) => response.json() )
        .then( (json) => {
            console.log('setState called');
            if (this.mounted === true){
               this.setState({
                  uid: json.users[0].user_id,
                  bid: json.users[0].building_id,
                  username: json.users[0].username
              });
            }
        })
        .done();
},
我仍然不知道我们是否应该使用
TimerMixins
,但这种方法在没有这些的情况下是有效的。 (
TimerMixins
注意清除组件中设置的任何超时或间隔)

编辑:将样本更新为仅调用组件的
setState

我不知道是否有更好的方法,但据我所知,到目前为止,您无法取消提取请求。

是否有可能在网络调用返回之前卸载组件?这是在
立即resetroutestack()中调用的路由。
这可能是问题所在吗?啊,是的,提取请求没有终止。我会用一个新的建议更新我的答案,我会像你一样对代码进行注释。再次在辛恩布罗特举行Thanx活动