2017-01-16 60 views

回答

3

是的。

一旦你的JavaScript加载,你可以通过渲染你更换Loading...反应应用到同一<div>这样使用component生命周期方法的

render(
    <App />, 
    document.getElementById('content') 
); 
0

的一种方式。

class App extends React.Component { 
 
    constructor(props){ 
 
     super(props); 
 
     this.state = { 
 
     loading: true 
 
     }; 
 
    } 
 
    
 
    componentWillMount(){ 
 
     this.setState({loading: true}); //optional 
 
    } 
 

 
    componentDidMount(){ 
 
     this.setState({loading: false}) 
 
    } 
 
    
 
    render() { 
 
     return (
 
      <section className="content"> 
 
       {this.state.loading && 'loading...'} {/*You can also use custom loader icon*/} 
 
       <p>Your content comes here</p> 
 
       {this.props.children} 
 
      </section> 
 
     ); 
 
    } 
 
} 
 

 
ReactDOM.render(<App />, document.getElementById("app"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 
 
<div id="app"> 
 
    
 
</div>

+0

这是很酷,但我想显示的图标ReactDOM获取呈现前。我将把我的加载器图标放在#app div中。 – Michal

+0

当你的'loading'组件再次成为'react'组件时,这将有所帮助。这可能是比取决于HTML绑定更好的方法。 –

+0

@JyothiBabuAraja如果您希望在客户端在缓慢的网络上下载资产时向客户端显示加载状态,该怎么办? –

相关问题