React生命周期

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




  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <Meta charset="UTF-8" />
  5. <title>React生命周期</title>
  6. <script src="https://unpkg.com/react@16/umd/react.development.js"></script>
  7. <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
  8. <script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
  9. </head>
  10. <body>
  11. <div id="root"></div>
  12. <script type="text/babel">
  13. class Components extends React.Component {
  14.  
  15. constructor(props){
  16. super(props);
  17. this.state = {}
  18. }
  19. componentWillMount(){
  20. console.log("实例化:componentWillMount")
  21. }
  22. componentDidMount(){
  23. console.log("实例化:componentDidMount")
  24. }
  25. componentWillReceiveProps(){
  26. console.log("存在期:componentWillReceiveProps")
  27. }
  28. shouldComponentUpdate(nextProps,nextState){
  29. console.log("存在期:shouldComponentUpdate",nextProps,nextState)
  30. return true;
  31. }
  32. componentWillUpdate(){
  33. console.log("存在期:componentWillUpdate")
  34. }
  35. componentDidUpdate(){
  36. console.log("存在期:componentDidUpdate")
  37. }
  38. render() {
  39. if(!this.props.reRender){
  40. console.log("实例化:render")
  41. }else{
  42. console.log("存在期:render")
  43. }
  44. return (
  45. <div>
  46. <br />
  47. 请查看下面的console
  48. <br />
  49. </div>
  50. )
  51.  
  52. }
  53. }
  54. Components.defaultProps = {
  55. text: "hello word",}
  56. class App extends React.Component{
  57. constructor(props){
  58. super(props);
  59. this.state = {}
  60. }
  61. refresh(){
  62. return (e)=>{
  63. this.setState({
  64. reRender: true,})
  65. }
  66. }
  67. render(){
  68. return (
  69. <div>
  70. <Components reRender={this.state.reRender}/>
  71. <button onClick={this.refresh()}>
  72. 更新Component
  73. </button>
  74. </div>
  75. )
  76. }
  77. }
  78. ReactDOM.render(<App />,document.getElementById('root'));
  79. </script>
  80. </body>
  81. </html>

猜你在找的React相关文章