2017-01-16 59 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如果您希望在客戶端在緩慢的網絡上下載資產時向客戶端顯示加載狀態,該怎麼辦? –

相關問題