1
在浏览器下载所有js文件并加载反应应用程序之前,显示加载程序图标的标准方式是什么?在首次反应应用初始化之前显示加载图标
我可以做这样的事吗?
<div id="content" class="app">
Loading...
</div>
在浏览器下载所有js文件并加载反应应用程序之前,显示加载程序图标的标准方式是什么?在首次反应应用初始化之前显示加载图标
我可以做这样的事吗?
<div id="content" class="app">
Loading...
</div>
是的。
一旦你的JavaScript加载,你可以通过渲染你更换Loading...
反应应用到同一<div>
这样使用component
生命周期方法的
render(
<App />,
document.getElementById('content')
);
的一种方式。
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>
这是很酷,但我想显示的图标ReactDOM获取呈现前。我将把我的加载器图标放在#app div中。 – Michal
当你的'loading'组件再次成为'react'组件时,这将有所帮助。这可能是比取决于HTML绑定更好的方法。 –
@JyothiBabuAraja如果您希望在客户端在缓慢的网络上下载资产时向客户端显示加载状态,该怎么办? –