简体   繁体   English

ReactJS在等待数据时加载图标

[英]ReactJS Loading icon while waiting for data

I'm experimenting with ReactJS, and much of my application relies on loading data from an API, using jQuery to help me with that. 我正在试验ReactJS,我的很多应用程序依赖于从API加载数据,使用jQuery来帮助我。

I have loading text/icon setup, but it feels a bit hacky and not very reusable: 我已加载文本/图标设置,但感觉有点hacky并且不是非常可重用:

ListWrapper = React.createClass({
    getInitialState: function() {
        return {
            loading: true,
            listData: []
        };
    },
    componentDidMount: function() {
        Api.get('/v1/listdata', function (data) {
            react.setState({
                    loading: false,
                    listData: data
            });
        });
    },
    render: function() {
        return (
            <List loading={this.state.loading} data={this.state.listData} />
        );
    }
});

List = React.createClass({
    render: function() {

        var mapData;
        if (this.props.loading)
            mapData = <Loader />; 
        else {
            mapData = this.props.data.map(function(item) {
                return (
                    <ListItem name={item.name} />
                );
            });
        }

        return (
            <div>
                {mapData}
            </div>
        );
    }
});

This works fine, however the whole if/else statement has hints of code smells since I'd have to do that logic all over the place. 这工作正常,但整个if / else语句都有代码味道的提示,因为我必须在整个地方做这个逻辑。

Can't really find anything on the web about how to best approach this... Whats a better way? 无法在网上找到关于如何最好地接近这一点的任何内容...什么是更好的方法?

You can simplify your existing code to be less verbose just by removing the else statement: 只需删除else语句,就可以简化现有代码,使其更简洁:

render: function() {
        if (this.props.loading) {
            return <Loader />;
        }
        var mapData = this.props.data.map(function(item) {
            return (
                <ListItem name={item.name} />
             );
        });
        return (
            <div>
                {mapData}
            </div>
        );
    }

But one solution I can think of is have your component accept a isActive bool and moving the if logic into the loader component instead of the individual components. 但我能想到的一个解决方案是让你的组件接受一个isActive bool并将if逻辑移动到加载器组件而不是单个组件。 When isActive = true it shows the spinner/loading icon. isActive = true它显示微调器/加载图标。 Otherwise it just returns a null component or empty . 否则它只返回一个null组件或为空。

List = React.createClass({
    render: function() {

        var mapData = this.props.data.map(function(item) {
            return (
                <ListItem name={item.name} />
            );
        });
        return (
            <div>
                <Loader isActive={this.props.isLoading} />
                {mapData}
            </div>
        );
    }
});

This case instead of doing the if logic everywhere, you just do it once in your Loader component, and it just returns null/empty if its inactive. 这种情况不是在任何地方执行if逻辑,而是在Loader组件中执行一次,如果它处于非活动状态,它只返回null / empty。

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

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