在渲染下一个组件之前在同一页面上进行全局加载

时间:2019-08-02 11:25:59

标签: reactjs react-loadable

在反应中,我需要一个条形加载,该加载将出现在当前页面的顶部,直到完全呈现下一个请求的组件。这是我真正想要的示例,https://www.bd.airtel.com/en

enter image description here

1 个答案:

答案 0 :(得分:1)

有很多可用的库。为了满足您的确切要求,请查看此react-top-loading-bar

import React, { Component } from 'react';

import LoadingBar from 'react-top-loading-bar';

export default class App extends Component {
  state = {
    loadingBarProgress: 0
  }

  add = value => {
    this.setState({
      loadingBarProgress: this.state.loadingBarProgress + value
    })
  }

  complete = () => {
    this.setState({ loadingBarProgress: 100 })
  }

  onLoaderFinished = () => {
    this.setState({ loadingBarProgress: 0 })
  }

  render() {
    return (
      <div>
        <LoadingBar
          progress={this.state.loadingBarProgress}
          height={3}
          color='red'
          onLoaderFinished={() => this.onLoaderFinished()}
        />
        <button onClick={() => this.add(10)}>Add 10</button>
        <button onClick={() => this.add(30)}>Add 30</button>
        <button onClick={() => this.complete()}>Complete</button>
      </div>
    )
  }
}