ReactJS组件的生命周期

前端之家收集整理的这篇文章主要介绍了ReactJS组件的生命周期前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

为了理解ReactJS中的组件的生命周期,我们通过下面的示例代码来直观的感受一下,当我们的React组件的整个生命周期都发生了哪些事件。直接上代码和运行结果吧!

  1. .main { padding: 10px 50px; }
  2.  
  3. .log { padding: 5px; border-bottom: 1px solid #ccc; }
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5. <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
  6. <script src="http://libs.cdnjs.net/react/0.12.0/react.min.js"></script>
  7. <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
  8. <link rel="stylesheet" href="style.css" />
  9. </head>
  10.  
  11. <body>
  12. <div class="main">
  13. <h1>Understanding the React Component Lifecycle</h1>
  14. <div id="app"></div>
  15. <hr />
  16. <button type="button" id="unmount" class="btn btn-danger">Unmount</button>
  17. <hr />
  18. <div id="screen"></div>
  19. </div>
  20. <script src="script.js"></script>
  21. </body>
  22.  
  23. </html>
  1. writeToScreen('Initial','primary');
  2.  
  3. var Welcome = React.createClass({
  4. getInitialState: function() {
  5. writeToScreen('GetInitialState','info');
  6. return {foo : 1};
  7. },getDefaultProps: function() {
  8. writeToScreen('GetDefaultProps','info');
  9. return {bar: 2};
  10. },update: function() {
  11. writeToScreen('Updating State','primary');
  12. this.setState({foo: 2});
  13. },render: function() {
  14. writeToScreen('Render','success');
  15. return (<div>
  16. This.state.foo: {this.state.foo} <br />
  17. This.state.bar: {this.props.bar}
  18. <br/>
  19. <hr/>
  20. <button className="btn btn-success"
  21. onClick={this.update}>
  22. Update State
  23. </button>
  24. </div>);
  25. },componentWillMount: function() {
  26. writeToScreen('ComponentWillMount','warning');
  27. },componentDidMount: function() {
  28. writeToScreen('ComponentDidMount',shouldComponentUpdate: function() {
  29. writeToScreen('ShouldComponentUpdate','info');
  30. return true;
  31. },componentWillReceiveProps: function(nextProps) {
  32. writeToScreen('ComponentWillRecieveProps',componentWillUpdate: function() {
  33. writeToScreen('ComponentWillUpdate',componentDidUpdate: function() {
  34. writeToScreen('ComponentDidUpdate',componentWillUnmount: function() {
  35. writeToScreen('componentWillUnmount','danger');
  36. }
  37. });
  38.  
  39. var App = React.createClass({
  40. getInitialState: function() {
  41. return {id: 1};
  42. },update: function() {
  43. writeToScreen('Updating Props','primary');
  44. this.setState({id: 2});
  45. },render: function() {
  46. return (
  47. <div>
  48. <hr/>
  49. <Welcome bar={this.state.id} />
  50. <hr />
  51. <button type="button" className="btn btn-primary"
  52. onClick={this.update}>
  53. Update Props
  54. </button>
  55. </div>
  56. )
  57. }
  58. });
  59.  
  60. React.render(
  61. <App />,document.getElementById('app')
  62. );
  63.  
  64. var unmountBtn = document.getElementById('unmount');
  65. unmountBtn.addEventListener('click',unmount);
  66.  
  67. function unmount() {
  68. writeToScreen('Unmounting','primary');
  69. React.unmountComponentAtNode(document.getElementById('app'));
  70. unmountBtn.remove();
  71. }
  72.  
  73. function writeToScreen(msg,level) {
  74. var elem = document.getElementById('screen');
  75. elem.innerHTML += '<div class="log bg-' + level + '">' +
  76. '<span class="glyphicon glyphicon-ok"></span> &nbsp;&nbsp;' +
  77. msg +
  78. '</div>';
  79. }
  • 运行结果:

    1. Initial

    2. UpdateProps

    3. UpdateState

    4. unmount

    5. whole

猜你在找的React相关文章