react 生命周期及参数

export interface ChilderPropsInteface {
  propsName?: string
}
export interface ChilderStateInterface {
  stateName?: string
}
export interface ChilderContext {
  contextName?: string
}

class Childer extends React.Component<
  ChilderPropsInteface,
  ChilderStateInterface,
  ChilderContext
> {
  // 构造函数
  constructor(props: ChilderPropsInteface) {
    const context: ChilderContext = {}
    super(props, context)
  }
  // 代替componentWillReceiveProps()。
  // 老版本中的componentWillReceiveProps()方法判断前后两个 props 是否相同,如果不同再将新的 props 更新到相应的 state 上去。
  // 这样做一来会破坏 state 数据的单一数据源,导致组件状态变得不可预测,另一方面也会增加组件的重绘次数。
  static getDerivedStateFromProps(
    nextProps: ChilderPropsInteface,
    prevState: ChilderStateInterface
  ): ChilderPropsInteface & ChilderStateInterface {
    const newState: ChilderPropsInteface & ChilderStateInterface = {
      propsName: '1',
      stateName: '3',
    }
    return newState
  }
  // 一般用的比较少,它更多的是在服务端渲染时使用。它代表的过程是组件已经经历了constructor()初始化数据后,但是还未渲染DOM时
  // 16.9
  // componentWillMount(): void {
  //   console.log('componentWillMount')
  // }
  UNSAFE_componentWillMount(): void {
    console.log('UNSAFE_componentWillMount')
  }
  // 组件第一次渲染完成,此时dom节点已经生成,可以在这里调用ajax请求,返回数据setState后组件会重新渲染
  componentDidMount(): void {
    console.log('componentDidMount')
  }
  // 在此处完成组件的卸载和数据的销毁。
  // clear你在组建中所有的setTimeout,setInterval
  // 移除所有组建中的监听 removeEventListener
  // 有时候我们会碰到这个warning:
  componentWillUnmount(): void {
    console.log('componentWillUnmount')
  }
  // 在接受父组件改变后的props需要重新渲染组件时用到的比较多
  // 接受一个参数nextProps
  // 通过对比nextProps和this.props,将nextProps的state为当前组件的state,从而重新渲染组件
  // componentWillReceiveProps(
  //   nextProps: ChilderPropsInteface,
  //   nextContext: ChilderContext
  // ): void {
  //   console.log('componentWillReceiveProps')
  // }
  UNSAFE_componentWillReceiveProps(
    nextProps: ChilderPropsInteface,
    nextContext: ChilderContext
  ): void {
    console.log('componentWillReceiveProps')
  }
  // 主要用于性能优化(部分更新)
  // 唯一用于控制组件重新渲染的生命周期,由于在react中,setState以后,state发生变化,组件会进入重新渲染的流程,在这里return false可以阻止组件的更新
  // 因为react父组件的重新渲染会导致其所有子组件的重新渲染,这个时候其实我们是不需要所有子组件都跟着重新渲染的,因此需要在子组件的该生命周期中做判断
  shouldComponentUpdate(
    nextProps: ChilderPropsInteface,
    nextState: ChilderStateInterface,
    nextContext: ChilderContext
  ): boolean {
    console.log('shouldComponentUpdate')
    return true
  }
  // shouldComponentUpdate返回true以后,组件进入重新渲染的流程,进入componentWillUpdate,这里同样可以拿到nextProps和nextState。
  // componentWillUpdate(
  //   nextProps: ChilderPropsInteface,
  //   nextState: ChilderStateInterface,
  //   nextContext: ChilderContext
  // ): void {
  //   console.log('componentWillUpdate')
  // }
  UNSAFE_componentWillUpdate(
    nextProps: ChilderPropsInteface,
    nextState: ChilderStateInterface,
    nextContext: ChilderContext
  ): void {
    console.log('UNSAFE_componentWillUpdate')
  }
  // 组件更新完毕后,react只会在第一次初始化成功会进入componentDidmount,之后每次重新渲染后都会进入这个生命周期,这里可以拿到prevProps和prevState,即更新前的props和state。
  componentDidUpdate(
    prevProps: ChilderPropsInteface,
    prevState: ChilderStateInterface,
    snapshot: ChilderContext
  ): void {
    console.log('componentDidUpdate')
  }
  // 代替componentWillUpdate。
  // 常见的 componentWillUpdate 的用例是在组件更新前,读取当前某个 DOM 元素的状态,并在 componentDidUpdate 中进行相应的处理。
  getSnapshotBeforeUpdate(
    prevProps: ChilderPropsInteface,
    prevState: ChilderStateInterface
  ): ChilderContext | null {
    const snapshot: ChilderContext = {
      contextName: '222',
    }
    console.log('getSnapshotBeforeUpdate')
    return snapshot
  }
  // 当有错误发生时, 我们可以友好地展示 fallback 组件;
  // 可以捕捉到它的子元素(包括嵌套子元素)抛出的异常;
  // 可以复用错误组件;
  componentDidCatch(error: Error, errorInfo: ErrorInfo): void {
    console.log('componentDidCatch')
  }
  // render函数会插入jsx生成的dom结构,react会生成一份虚拟dom树,在每一次组件更新时,在此react会通过其diff算法比较更新前后的新旧DOM树,比较以后,找到最小的有差异的DOM节点,并重新渲染。
  render() {
    return <div>Childer</div>
  }
}

  


export interface ChilderPropsInteface {
propsName?: string
}
export interface ChilderStateInterface {
stateName?: string
}
export interface ChilderContext {
contextName?: string
}

class Childer extends React.Component<
ChilderPropsInteface,
ChilderStateInterface,
ChilderContext
> {
// 构造函数
constructor(props: ChilderPropsInteface) {
const context: ChilderContext = {}
super(props, context)
}
// 代替componentWillReceiveProps()。
// 老版本中的componentWillReceiveProps()方法判断前后两个 props 是否相同,如果不同再将新的 props 更新到相应的 state 上去。
// 这样做一来会破坏 state 数据的单一数据源,导致组件状态变得不可预测,另一方面也会增加组件的重绘次数。
static getDerivedStateFromProps(
nextProps: ChilderPropsInteface,
prevState: ChilderStateInterface
): ChilderPropsInteface & ChilderStateInterface {
const newState: ChilderPropsInteface & ChilderStateInterface = {
propsName: '1',
stateName: '3',
}
return newState
}
// 一般用的比较少,它更多的是在服务端渲染时使用。它代表的过程是组件已经经历了constructor()初始化数据后,但是还未渲染DOM时
// 16.9
// componentWillMount(): void {
// console.log('componentWillMount')
// }
UNSAFE_componentWillMount(): void {
console.log('UNSAFE_componentWillMount')
}
// 组件第一次渲染完成,此时dom节点已经生成,可以在这里调用ajax请求,返回数据setState后组件会重新渲染
componentDidMount(): void {
console.log('componentDidMount')
}
// 在此处完成组件的卸载和数据的销毁。
// clear你在组建中所有的setTimeout,setInterval
// 移除所有组建中的监听 removeEventListener
// 有时候我们会碰到这个warning:
componentWillUnmount(): void {
console.log('componentWillUnmount')
}
// 在接受父组件改变后的props需要重新渲染组件时用到的比较多
// 接受一个参数nextProps
// 通过对比nextProps和this.props,将nextProps的state为当前组件的state,从而重新渲染组件
// componentWillReceiveProps(
// nextProps: ChilderPropsInteface,
// nextContext: ChilderContext
// ): void {
// console.log('componentWillReceiveProps')
// }
UNSAFE_componentWillReceiveProps(
nextProps: ChilderPropsInteface,
nextContext: ChilderContext
): void {
console.log('componentWillReceiveProps')
}
// 主要用于性能优化(部分更新)
// 唯一用于控制组件重新渲染的生命周期,由于在react中,setState以后,state发生变化,组件会进入重新渲染的流程,在这里return false可以阻止组件的更新
// 因为react父组件的重新渲染会导致其所有子组件的重新渲染,这个时候其实我们是不需要所有子组件都跟着重新渲染的,因此需要在子组件的该生命周期中做判断
shouldComponentUpdate(
nextProps: ChilderPropsInteface,
nextState: ChilderStateInterface,
nextContext: ChilderContext
): boolean {
console.log('shouldComponentUpdate')
return true
}
// shouldComponentUpdate返回true以后,组件进入重新渲染的流程,进入componentWillUpdate,这里同样可以拿到nextProps和nextState。
// componentWillUpdate(
// nextProps: ChilderPropsInteface,
// nextState: ChilderStateInterface,
// nextContext: ChilderContext
// ): void {
// console.log('componentWillUpdate')
// }
UNSAFE_componentWillUpdate(
nextProps: ChilderPropsInteface,
nextState: ChilderStateInterface,
nextContext: ChilderContext
): void {
console.log('UNSAFE_componentWillUpdate')
}
// 组件更新完毕后,react只会在第一次初始化成功会进入componentDidmount,之后每次重新渲染后都会进入这个生命周期,这里可以拿到prevProps和prevState,即更新前的props和state。
componentDidUpdate(
prevProps: ChilderPropsInteface,
prevState: ChilderStateInterface,
snapshot: ChilderContext
): void {
console.log('componentDidUpdate')
}
// 代替componentWillUpdate。
// 常见的 componentWillUpdate 的用例是在组件更新前,读取当前某个 DOM 元素的状态,并在 componentDidUpdate 中进行相应的处理。
getSnapshotBeforeUpdate(
prevProps: ChilderPropsInteface,
prevState: ChilderStateInterface
): ChilderContext | null {
const snapshot: ChilderContext = {
contextName: '222',
}
console.log('getSnapshotBeforeUpdate')
return snapshot
}
// 当有错误发生时, 我们可以友好地展示 fallback 组件;
// 可以捕捉到它的子元素(包括嵌套子元素)抛出的异常;
// 可以复用错误组件;
componentDidCatch(error: Error, errorInfo: ErrorInfo): void {
console.log('componentDidCatch')
}
// render函数会插入jsx生成的dom结构,react会生成一份虚拟dom树,在每一次组件更新时,在此react会通过其diff算法比较更新前后的新旧DOM树,比较以后,找到最小的有差异的DOM节点,并重新渲染。
render() {
return <div>Childer</div>
}
}
原文地址:https://www.cnblogs.com/tongchuanxing/p/15466686.html