一. ref 的使用 ( 直接获取 DOM 元素 )

  在 input 标签上 可以使用 ref 属性 获取当前DOM节点

  eg:

    import React , { Component, Fragment } from 'react';

    class MyComponent extends Component {

      constructor(props){

        super(props);

        this.handleInput = this.handleInput.bind(this);

        this.state = { arr = [] }

      }

      render(){

        return (

          <Fragment>

          // input 为当前的 DOM 节点 将 this.input 指向该节点 方便引用 相当于 e.target

          <input ref={(input)=>{this.input=input}} />

          <button onClick={this.handleInput}>提交</button>

          </Fragment>

        )

      }

      handleInput(){

        console.log(this.input.value);

        this.setState((prevProp)=>({

          arr : [...prevProp.arr, this.input.value]

        }), ()=>{

          // 该段代码 为异步操作 ( setState ) 之后执行的代码

          console.log(this.state.arr.length)

        })

      }

    }

    export default MyComponent;

二.  React 生命周期函数

  生命周期函数是指在某一个时刻组件会自动调用执行的函数

  1. Initialization  初始化

    设置 props 和 state

    // 因为所有的 类内部都有 该方法 并不是 react 的生命周期函数

    constructor(props){

      super(props);

      this.status = {  }

    }

  2. Mounting  挂载

    // 在组件即将被挂载到页面的时刻自动执行 (挂载前)

    // 16.3 将 componentWillMount 更名为 UNSAFE_componentWillMount

    // 17.* 将 删除 componentWillMount

    componentWillMount(){

      console.log('componentWillMount');

    }

    // 渲染页面

    render(){

      console.log('render')

    }

    // 在组件被挂载到页面后自动执行 (挂载后)

    componentDidMount(){

      console.log('componentDidMount')

    }

  3. updation  数据更新

    props 更新

      // 当一个组件 从父组件接受了参数

      // 子组件 第一次 出现在父组件中 不会被执行

      // 子组件 第一次后 出现在父组件中 会被执行

      // 16.3 将 componentWillReceiveProps 更名为 UNSAFE_componentWillReceiveProps

      // 17.* 将 删除 componentWillReceiveProps

      componentWillReceiveProps(){

        console.log('child componentWillReceiveProps');

      }

    props 和 state 共同更新执行的生命周期函数

      // 组件被更新前,会自动执行 判断是否更新  返回 bool 类型的结果 true 为 更新 (后面的函数会执行)  false 为不更新 (则后面的函数不会执行)

      shouldComponentUpdate(){

        console.log('sholdComponentUpdate');

        return true;

      }

      //  组件被更新前,会自动执行

      // 16.3 将 componentWillUpdate 更名为 UNSAFE_componentWillUpdate

      // 17.* 将 删除 componentWillUpdate

      componentWillUpdate(){

        console.log('componentWillUpdate');

      }

      // 渲染更新的 组件

      render(){

        console.log('render');

      }

      // 组件被更新之后 自动执行

      componentDidUpdate(){

        console.log('componentDidUpdate');

      }

  4. Unmounting  把组件去除

    // 将把组件从页面中剔除前执行 (去除挂载前)子组件 方法

    componentWillUnmount(){

      console.log('child componentWillUnmount');

    }

    

三. 生命周期函数使用场景

  1. 除了 render 函数 其他的生命周期函数都可以不存在

    // 原因 继承 Component 时 除了 render 函数 其他函数都有 默认函数支持

    // 所以要自定义编写 render 函数

  2. 当每次 state 改变时 render 函数都要被执行

    render 函数执行 会导致 子组件会被重新渲染

    但 只有特定条件下 state 的 值才有用 所以 使用

    shouldComponentUpdate 进行 代码优化 组织

    // 在子组件内编写

    // 获取将要变化的 props 和 state -- nextProps 和 nextState

      shouldComponentUpdate(nextProps, nextState){

      // 如果 下次传递过来渲染的值 不等于 上次的值则渲染 反之 不渲染

      return nextProps.value !== this.props.value;

    }

  3. 使用 获取远程数据 作为 基础数据时 使用 componentDidMount (挂载好后获取数据)

    componentDidMount(){

      this.ajax().then((res)=>{

      })

    }

  4. 发送 ajax 请求 时 使用

    安装 axios

      yarn add axios

    使用 axios

      import axios from 'axios';

      componentDidAount(){

        axios.get('/api/xxx')

          .then(()=>{alert('succ')})

          .catch(()=>{alert('err')})

      }

10. react 基础 ref 的使用 及 React 16 的生命周期函数 及 生命周期函数使用场景的更多相关文章

  1. React基础语法学习

    React主要有如下3个特点: 作为UI(Just the UI) 虚拟DOM(Virtual DOM):这是亮点 是React最重要的一个特性 放进内存 最小更新的视图,差异部分更新 diff算法 ...

  2. 36.React基础介绍——2019年12月24日

    2019年12月24日16:47:12 2019年10月25日11:24:29 主要介绍react入门知识. 1.jsx语法介绍 1.1 介绍 jsx语法是一种类似于html标签的语法,它的作用相当于 ...

  3. react基础(1)

    在 react入门系列 里面,介绍了一些react的基础知识,在react基础部分,会结合手脚架工具进行更多的总结. 关于webpack我在这里就不讲解了,有需要的小伙伴可以自己去百度一下学习资料,我 ...

  4. React 基础入门,基础知识介绍

    React不管在demo渲染还是UI上,都是十分方便,本人菜鸟试试学习一下,结合阮一峰老师的文章,写下一点关于自己的学习react的学习笔记,有地方不对的地方,希望各位大牛评论指出: PS:代码包下载 ...

  5. Flux --> Redux --> Redux React 基础实例教程

    本文的目的很简单,介绍Redux相关概念用法 及其在React项目中的基本使用 假设你会一些ES6.会一些React.有看过Redux相关的文章,这篇入门小文应该能帮助你理一下相关的知识 一般来说,推 ...

  6. react基础学习和react服务端渲染框架next.js踩坑

    说明 React作为Facebook 内部开发 Instagram 的项目中,是一个用来构建用户界面的优秀 JS 库,于 2013 年 5 月开源.作为前端的三大框架之一,React的应用可以说是非常 ...

  7. React基础篇学习

    到今天为止, 使用react已经一年了, 现在整理一下入门时的一些重要知识, 帮助想要学习react的同学们理解某些内容. React 元素 React 元素,它是 React 中最小基本单位,我们可 ...

  8. react基础总结

    React的特点: 1.声明式: 可以声明式的在js中写html结构: 注意: react是用很像 js 的语言写标签; const jsx = <div className="app ...

  9. react基础学习

    什么是react:React(有时称为React.js 或ReactJS)是一个为数据提供渲染HTML视图的开源JavaScript库; 其特点: 声明式设计:采用声明范式,可以轻松描述应用高效:通过 ...

随机推荐

  1. 云时代架构阅读笔记十一——数据库SQL优化

    网上关于SQL优化的教程很多,但是比较杂乱.近日有空整理了一下,写出来跟大家分享一下,其中有错误和不足的地方,还请大家纠正补充. 1.对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 ...

  2. 我的C语言的新开端<graphics.h>

    进一步接触C语言<graphics.h> #include<stdio.h> #include<graphics.h> #include<conio.h> ...

  3. 时间戳,秒级,毫秒级转换DateTime格式

    解决了本地时间和格林尼治时间差问题 function DateTimeToTp(ConvDate: TDateTime): time_t;var zi: TTimeZoneInformation;be ...

  4. Vue2.x双向数据绑定

    1.vue双向绑定原理 vue.js 则是采用数据劫持结合发布者-订阅者模式的方式,通过Object.defineProperty()来劫持各个属性的setter,getter,在数据变动时发布消息给 ...

  5. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-user

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  6. 01 DDL(DataDefinitionLanguage)

    注: 语句用 ; 或 \g \G 表示结束 .       建库语句 :         CREATE DATABASE db_name ;          查询有哪些库 :         SHO ...

  7. Wordpress自动更新失败

    Wordpress自动更新失败 一.手动升级Wordpress教程第一步:备份好Wordpress数据 在决定手动升级Wordpress前,你需要做好Wordpress的数据备份工作,以防手动升级失败 ...

  8. Elasticsearch全文搜索引擎-PHP使用教程。

    1.声明依赖关系:         比方说,你的项目中需要一个php版的elasticsearch框架.为了将它添加到你的项目中(下载),你所需要做的就是创建一个 composer.json 文件,其 ...

  9. oracle11g导出dmp文件时不能导出空表,导致缺表

    一.执行 select 'analyze table '||table_name||' compute statistics;' from user_tables; 将该查询语句查询到的结果粘贴到sq ...

  10. cudaThreadSynchronize()

    // 调用CUDA kernel 是非阻塞的,调用kernel语句后面的语句不等待kernel执行完,立即执行.所以在 call_kernel(see kernel.cu) 中执行 m5op.dump ...