美文网首页
React生命周期理解

React生命周期理解

作者: 充满正能量的灰灰 | 来源:发表于2018-07-04 21:40 被阅读0次

render:纯粹的返回值,方便看组件的作用

返回值:

*  **React elements.** 

*  **Arrays and fragments.**

*  **Portals**. return ReactDOM.createPortal(child, container)

*  **String and numbers.** These are rendered as text nodes in the DOM.

*  **Booleans or `null`**. Render nothing. (Mostly exists to support `return test && <Child />` pattern, where `test` is boolean.)

constructor(props,context):初始化state和绑定方法的this作用域

1、If you don’t initialize state and you don’t bind methods, you don’t need to implement a constructor for your React component.

2、Avoid copying props into state! This is a common mistake:

The problem is that it’s both unnecessary (you can use this.props.color directly instead), and creates bugs (updates to the color prop won’t be reflected in the state).

componentDidMount(): is invoked immediately after a component is mounted (inserted into the tree). Initialization that requires DOM nodes should go here. If you need to load data from a remote endpoint, this is a good place to instantiate the network request.

componentDidUpdate(prevProps,prevState,snapshot):is invoked immediately after updating occurs. This method is not called for the initial render. Use this as an opportunity to operate on the DOM when the component has been updated. This is also a good place to do network requests as long as you compare the current props to previous props (e.g. a network request may not be necessary if the props have not changed).

You may call setState() immediately in componentDidUpdate() but note that it must be wrapped in a condition like in the example above, or you’ll cause an infinite loop. It would also cause an extra re-rendering which, while not visible to the user, can affect the component performance. If you’re trying to “mirror” some state to a prop coming from above, consider using the prop directly instead.If your component implements the getSnapshotBeforeUpdate() lifecycle (which is rare), the value it returns will be passed as a third “snapshot” parameter to componentDidUpdate(). Otherwise this parameter will be undefined.

https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html(合理把握props和state的转换关系),组件实例-ref

 when designing a component, it is important to decide whether its data will be controlled or uncontrolled.

相关文章

  • React组件生命周期

    问题 理解React组件的生命周期 知识点 React流程状态图 注意:流程状态图为使用React.createC...

  • react 生命周期

    React 生命周期文档 1、理解 组件对象从创建到死亡它会经历特定的生命周期阶段 React组件对象包含一系列的...

  • 面试官:说说你对react生命周期的理解

    面试官:说说你对react生命周期的理解 hello,这里是潇晨,今天我们来看下react生命周期在各个阶段是怎样...

  • React概念图

    React概念图 React组件生命周期概念图 参考文档:React入门教程 组件生命周期React:组件生命周期...

  • 4.组件的生命周期

    了解React生命周期,对我们理解React的工作过程很有帮助。所有的事物都有自己的生命过程,React组件也不例...

  • React基础篇之组件的生命周期

    引出生命周期 react生命周期(旧) react生命周期(新) getSnapshotBeforeUpdate的...

  • React 生命周期初探

    概论 什么是生命周期 个人理解的生命周期就是一个对象从开始生成到最后销毁所经历的不同状态,React 生命周期可分...

  • React生命周期

    React v16.0前的生命周期 React v16.4+ 的生命周期图 React v16.9后这些生命周期钩...

  • React生命周期理解

    render:纯粹的返回值,方便看组件的作用 返回值: * **React elements.** * **A...

  • 理解React生命周期

    constructor React借用class类的constructor充当初始化钩子。在我们类扩展任何其他具有...

网友评论

      本文标题:React生命周期理解

      本文链接:https://www.haomeiwen.com/subject/zriyuftx.html