简体   繁体   English

React Native加载屏幕(条件渲染)

[英]React Native loading screen (conditional render)

I am trying to render a view in a React Native Android app when a HTTP call has completed. 我正在尝试在HTTP调用完成后在React Native Android应用中呈现视图。

My render method: 我的渲染方法:

render: function() {
    if (this.state.obj.name) {
        return this.renderPage();
    } else {
        return (<Loading />);
    }
}

< Loading /> contains simple View and Text elements which display "Loading". <Loading />包含显示“ Loading”的简单View和Text元素。

I call the network request in componentDidMount(): 我在componentDidMount()中调用网络请求:

componentDidMount: function() {
    var self = this;

    SomeService.getObjById(1).then(function(data) {

        var obj = JSON.parse(data._bodyText);

        self.setState({
            obj: obj
        });
    });
},

However, when I update the state and instantiate 'obj' (which now has a name prop) render is recalled, it returns render page but the view does not update. 但是,当我更新状态并实例化“ obj”(现在有一个名称prop)时,渲染被调用,它将返回渲染页面,但视图不会更新。

My renderPage method works when it is light like: 我的renderPage方法可以在以下情况下正常工作:

renderPage: function() {
    return (
        <View><Text>Hello</Text></View>
    )
}

But when it has more than say 6 lines of code it seems it doesn't update. 但是,当它有6行以上的代码时,它似乎并没有更新。

Any idea why event though the conditional returns different components when the call is finished, the actual view does not update? 知道为什么事件虽然在调用完成时条件返回不同的组件,但实际视图没有更新?

I solved this by making the request before the change of view. 我通过在更改视图之前发出请求来解决此问题。

This still didnt work but I added a setTimout(function(){}, 0) around the navigator.push() and this worked, as it pushed the 'push' to the end of the call stack. 这仍然没有用,但是我在navigator.push()周围添加了setTimout(function(){},0),并且由于将“ push”推到了调用堆栈的末尾而起作用了。

Not amazing, but works 并不令人惊讶,但有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM