在React中显示DOM之前显示加载状态的正确方法

时间:2016-04-28 06:45:14

标签: reactjs loading state

我只想知道,在加载AJAX的数据被提取之前显示加载状态的最佳方法是什么。我曾尝试在codepen上使用setInterval重现它。 Code

class App extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      isFetching: true,
    };
  }

  componentWillMount() {
    let that = this;
    setTimeout(function() {
      that.setState({
        isFetching: false,
      });
    }, 2000);
  }

  render() {
    return (
      <div>
        {this.state.isFetching ? 'Fetching' : 'Done'}
      </div>
    );
  }
}

React.render( <App / > , document.getElementById('root'));

0 个答案:

没有答案
相关问题