首页 > 其他分享 >React-cnblog

React-cnblog

时间:2023-02-05 10:25:01浏览次数:46  
标签:count render React state 组件 cnblog setState

1. 生命周期

  • 老版本16.3及以前

  • 新版本16.4 及以后

1.2 错误处理的生命周期

  • static getDerivedStateFormError
  • componentDidCatch

2. 父子通讯

  • 父传子
    • props
    • Prototype Methods
      • 即把子组件写成一个非受控组件,父组件定义ref,绑定给子组件
      • this.ref.current.方法名(参数) 父组件的数据就通过参数传递
  • 子传父
    • 给子组件提供事件,子组件需要传递时调用
    • 事件冒泡
      • 子组件发生某些事件需要改变父组件某些数据时
      • 事件通过冒泡传递给父组件,父组件通过事件处理函数处理

3. setState函数的执行是同步的还是异步的

  • 两种情况
    • 同步
      • 原生事件
      • setTimeOut
    • 异步
      • React的合成事件(绑定的事件)
      • 钩子函数,当当就是钩子函数,不包裹上述同步的情况
import React, { Component } from 'react'

export default class SetStateComponent extends Component {
  state= {
    count : 0
  }
  // 同步:原生事件和setTimeout 
  // componentDidMount() {
  //   setTimeout(()=>{
  //     this.setState({count: this.state.count+1});
  //     console.log(`this.state.count`, this.state.count);
  //   })
  //   const ele = document.getElementById('element');
  //   ele.onclick = ()=>{
  //     this.setState({count: this.state.count+1});
  //     console.log(`this.state.count`, this.state.count)
  //   }
  // }
  componentDidMount() {
    this.setState({count: this.state.count+1});
    console.log(`this.state.count`, this.state.count)
  }
  
  // 异步: react合成事件 生命周期钩子函数
  handleClick = ()=> { 
    this.setState({count: this.state.count+1});
    console.log(`this.state.count`, this.state.count)
  }
  render() {
    return (
      <div>
        <button id="element">add</button>
        <button onClick={this.handleClick}>add</button>
      </div>
    )
  }
}

4. renderProps

  • render prop 是指一种在React组件之间使用值为prop的函数共享代码的技术

  • 场景,一个组件中的state里面的值被多个其他组件使用(跨任意层级的)

  • 有点像redux的原理

import React, { Component } from 'react'

class Cat extends Component {
  render(){
    const mouse = this.props.mouse
    return (
      <img src="/favicon.ico" style={{position:"absolute",left:mouse.x,top: mouse.y}}/>
    )
  }
}

class Mouse extends Component {
  state= {
    x:0,
    y:0,
  }
  // 获取鼠标的x轴和y轴的信息
  handleMouseMove = (event)=>{
    const x = event.clientX;
    const y = event.clientY;
    //更新state 坐标值
    this.setState({
      x,
      y
    })
  }
  render(){
    return (
      <div style={{background:'red',height:'100vh'}} onm ouseMove={this.handleMouseMove}>
        { this.props.render(this.state) }
      </div>
    )
  }
}

export default class MoseMove extends Component {
  render() {
    return (
      <div>
        <h1>鼠标移动</h1>
        <Mouse render={mouse=>(
          <Cat mouse={mouse}/>
        )}/>
      </div>
    )
  }
}

5. React-router的路由模式

6.jsx

  • 一个语法糖,XML中可以写js语法
  • 通过babel/preset-react转换成React.createElement创建的虚拟dom
  • 虚拟dom再通过render函数转换成页面真实的dom

标签:count,render,React,state,组件,cnblog,setState
From: https://www.cnblogs.com/lingxin1123/p/17092945.html

相关文章

  • es6-cnblog
    1.forEach,forin,forofforEach数组使用,无返回值,无法break跳出forin数组,对象使用forof数组,对象,map,set,类数组(伪数组)共同点无下标,操作更简洁,效率高2.v......
  • javascript-cnblog
    1.原型和原型链1.1原型js中,函数可以有属性,每个函数都有一个特殊的属性叫做原型prototype(原型)1.2原型链当我们访问某个对象的属性时,如果该对象本身没有,就会到对象......
  • css基础-cnblog
    1.盒模型定义css规范的一个模块定义一个长方形的盒子包含内外边距,边框标准定义上分分类IE盒模型width=width+padding+borderheight=height+padding+bord......
  • 浏览器-cnblog
    事件流捕获目标冒泡回流和重绘减少回流和重绘的次数css样式一次性修改constroot=document.getElementById('root')root.style.cssText='padding:10px2......
  • 设计模式-cnblog
    设计模式1.装饰者模式不改变对象自身代码的基础上新增功能//装饰者模式//不改变对象自身代码的基础上新增功能leta=()=>{console.log('a');}//开闭......
  • webpack-cnblog
    1.webpack定义用于现代JavaScript的静态模块打包工具解决的问题模块依赖代码编写less转cssts转js开发效率热加载项目优化压缩,打包2.load......
  • 修改表时先删除后添加思想-cnblog
    4.3.2修改菜品信息4.3.2.1代码实现点击保存按钮,页面发送ajax请求,将修改后的菜品相关数据以json形式提交到服务端。在修改菜品信息时需要注意,除了要更新dish菜品表,还需......
  • 线程变量-cnblog
    1.3.2ThreadLocalThreadLocal并不是一个Thread,而是Thread的局部变量。当使用ThreadLocal维护变量时,ThreadLocal为每个使用该变量的线程提供独立的变量副本,所以每一个线程......
  • spring-data-redis-cnblog
    5.3SpringDataRedis5.3.1介绍SpringDataRedis是Spring的一部分,提供了在Spring应用中通过简单的配置就可以访问Redis服务,对Redis底层开发包进行了高度封装......
  • number类型id和String类型id处理-cnblog
    4.4功能测试代码编写完毕之后,我们需要将工程重启。然后访问前端页面,进行"启用"或"禁用"的测试。测试过程中没有报错,但是功能并没有实现,查看数据库中的数据也没有......