React中文教程 - Component Lifecycle(组件的生命周期)

前端之家收集整理的这篇文章主要介绍了React中文教程 - Component Lifecycle(组件的生命周期)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <Meta http-equiv='Content-type' content='text/html; charset=utf-8'>
  5. <title>React | Component Lifecycle</title>
  6. <script src="build/react.min.js"></script>
  7. <script src="build/JSXTransformer.js"></script>
  8. <style>h1{font-size:30px;}</style>
  9. </head>
  10. <body>
  11. <script type="text/jsx">
  12. /** @jsx React.DOM */
  13. React.renderComponent(<div>Hello,world!</div>,document.body);
  14. setTimeout(function() {
  15. React.renderComponent(<div>Goodbye,world.</div>,document.body);
  16. },3000);
  17. setTimeout(function() {
  18. React.renderComponent(<img src="/images/fin.png" />,2000);
  19. setTimeout(function() {
  20. React.unmountAndReleaseReactRootNode(document.body);
  21. React.renderComponent(<h1>Hi! EveryBody.</h1>,4000);
  22. </script>
  23. </body>
  24. </html>

React的组件生命周期分别是:Mounting(附加)、Updating(更新)、Unmounting(移出)

  • 使用React.renderComponent()函数把组件附件到已经存在的DOM节点
  • React.renderComponent判断被附加的DOM节点是否已经存在该类型组件,如果存在则更新,否则就做为新组件附件到DOM节点
  • 使用React.unmountAndReleaseReactRootNode()函数直接把当前DOM节点的全部组件移除,这样能避免内存暴增而导致浏览器崩溃


您可以修改并重新发布本文,如果您能留下本文的参考连结,万分谢谢! 如果您对本文存在疑问,欢迎留言或者直接对本文评论,我会在看到的第一时间回复您。

猜你在找的React相关文章