The previous lesson introduced the React component lifecycle mounting and unmounting. In this lesson you will learn some simple uses for these hooks.

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3.  
  4. class App extends React.Component {
  5. constructor(){
  6. super();
  7. this.state = {
  8. val:
  9. }
  10. }
  11. update(){
  12. this.setState({val: this.state.val + })
  13. }
  14. componentWillMount(){
  15. console.log(this.state)
  16. this.setState({
  17. val: this.state.val *
  18. });
  19. console.log("Component Will Mount");
  20. }
  21. render() {
  22. console.log("rendering");
  23. return (
  24. <div>
  25. <button onClick={this.update.bind(this)}>{this.state.val}</button>
  26. </div>
  27. )
  28. }
  29. componentDidMount(){
  30. this.inc = setInterval(this.update.bind(this), );
  31. console.log("Component Did Mount");
  32. }
  33. componentWillUnmount(){
  34. clearInterval(this.inc);
  35. console.log("Component will unmount");
  36. }
  37. }
  38.  
  39. export default class Wrapper extends React.Component{
  40. constructor(){
  41. super();
  42. }
  43. mount(){
  44. ReactDOM.render(<App />, document.getElementById('a'));
  45. }
  46. unmount(){
  47. // Unmount a dom node
  48. ReactDOM.unmountComponentAtNode(document.getElementById('a'))
  49. }
  50. render() {
  51. return (
  52. <div>
  53. <button onClick={this.mount.bind(this)}>Mount</button>
  54. <button onClick={this.unmount.bind(this)}>Unmount</button>
  55. <div id="a"></div>
  56. </div>
  57. );
  58. }
  59.  
  60. }

[React Fundamentals] Component Lifecycle - Mounting Usage的更多相关文章

  1. [React ] React Fundamentals: Component Lifecycle - Mounting Usage

    The previous lesson introduced the React component lifecycle mounting and unmounting. In this lesson ...

  2. [React Fundamentals] Component Lifecycle - Mounting Basics

    React components have a lifecycle, and you are able to access specific phases of that lifecycle. Thi ...

  3. [React] React Fundamentals: Component Lifecycle - Mounting Basics

    React components have a lifecycle, and you are able to access specific phases of that lifecycle. Thi ...

  4. [React Fundamentals] Component Lifecycle - Updating

    The React component lifecycle will allow you to update your components at runtime. This lesson will ...

  5. [React] React Fundamentals: Component Lifecycle - Updating

    The React component lifecycle will allow you to update your components at runtime. This lesson will ...

  6. React.js Tutorial: React Component Lifecycle

    Introduction about React component lifecycle. 1 Lifecycle A React component in browser can be any of ...

  7. React (Native) Rendering Lifecycle

    How Does React Native Work? The idea of writing mobile applications in JavaScript feels a little odd ...

  8. React & styled component

    React & styled component https://www.styled-components.com/#your-first-styled-component tagged t ...

  9. react slot component with args

    react slot component with args how to pass args to react props child component https://codesandbox.i ...

随机推荐

  1. 十个免费的Web压力测试工具

    两天,jnj在本站发布了<如何在低速率网络中测试 Web 应用>,那是测试网络不好的情况.而下面是十个免费的可以用来进行Web的负载/压力测试的工具,这样,你就可以知道你的服务器以及你的W ...

  2. BZOJ2542: [Ctsc2001]终极情报网

    题解: 乘积最小只需要取对数.然后反向边就变成1/c,而不是-c了. 精度问题搞得我已经我想说什么了... 贴一份网上的pascal 代码: type ss=record x,y,c,r,next:l ...

  3. vijos1603迷宫

    这题的构思太巧妙了: 经典题目8 给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值    把给定的图转为邻接矩阵,即A(i,j)=1当且仅当存在一条边i->j. ...

  4. Vim Vundle 插件管理器

    /********************************************************************** * Vim Vundle 插件管理器 * 说明: * 话 ...

  5. centos nginx 多端口配置过程记录

    1. 编辑 /usr/local/nginx/vhosts/  在此目录下增加一文件,如;ci.ainux.com,或复制一个文件 修改其中的端口和目录,更改log_format 名称 重启nginx ...

  6. 【 D3.js 高级系列 — 4.0 】 矩阵树图

    矩阵树图(Treemap),也是层级布局的扩展,根据数据将区域划分为矩形的集合.矩形的大小和颜色,都是数据的反映.许多门户网站都能见到类似图1,将照片以不同大小的矩形排列的情形,这正是矩阵树图的应用. ...

  7. Azure HDInsight与Hadoop周边系统集成

     Sunwei 9 Dec 2014 1:54 AM 传统的Hadoop系统提供给用户2个非常优秀的框架,MR计算框架和HDFS存储框架,尽管MR已经显得有些老迈而缓慢,但是HDFS还是很多应用系统的 ...

  8. 【转】ubuntu 编码 UTF-8 GBK GB18030

    添加编码支持 sudo locale-gen zh_CN.GBK sudo locale-gen zh_CN.GB2312 sudo locale-gen zh_CN.GB18030 2.更新一下lo ...

  9. init进程学习

    linux的init进程 一个在线编辑markdown文档的编辑器,是内核启动的第一个进程,init进程有很多重要的任务,它的pit 为1,在linux shell中使用pstree命令可以看到它为其 ...

  10. Esper系列(十四)Contained-Event Selection

    功能:该语法是针对所查询事件中的属性又是另一种属性的查询结果控制. 格式: 1  "+j); 19      bean.setBean(item); 20      list.add(bea ...