首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于反应生命周期方法(如componentWillReceiveProps )的典型用例是什么?

用于反应生命周期方法(如componentWillReceiveProps )的典型用例是什么?
EN

Stack Overflow用户
提问于 2016-08-13 06:42:20
回答 3查看 1.9K关注 0票数 1

componentWillReceiveProps和其他生命周期方法似乎具有欺骗性的诱惑力,将不必要的复杂性和噪声引入到缺乏经验的反应性编码器手中。它们为什么会存在?他们最典型的用例是什么?在不确定的时刻,我如何知道答案是否在于生命周期方法?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-08-13 10:50:12

我已经使用react几个月了,我的大部分工作都是从零开始创建一个大型应用程序。因此,同样的问题在一开始就出现了。

下面的信息是基于开发过程中的学习和通过多个文档来获得正确的信息。

正如问题中所问到的,下面是react中生命周期方法的几个用例。

  1. componentWillMount()
代码语言:javascript
复制
- This is called once on the server side, if server side rendering is present, and once the client side. 
- I personally have used it just to do api calls which do not have direct effect on the components, for example getting oAuth tokens

  1. componentDidMount()
代码语言:javascript
复制
- This function is mostly used for calling API's [(here](https://stackoverflow.com/questions/27139366/why-do-the-react-docs-recommend-doing-ajax-in-componentdidmount-not-componentwi) is why to call it in componentDidMount and not in componentWillMount)
- Components `state` initialisations which are based on the props passed by parents.

  1. componentWillReceiveProps(nextProps,nextState)
代码语言:javascript
复制
- This function is called every time props are received **except the first render**
- Most common use I have encountered is to update the _state_ of my current component which i can not do it in componentWillUpdate. 

  1. shouldComponentUpdate(nextProps, nextState)
代码语言:javascript
复制
- This method is invoked before the render happens when new props or states are received. Here we can return false if the re-render is not required.
- I see this as a performance optimisation tool. In case of frequent re-rendering of parent component this method should be used to avoid unnecessary update to current component   

  1. componentWillUpdate(nextProps,nextState)
代码语言:javascript
复制
- this function is called every time a component is updated, it is not called when component mounts
- Carry out any data processing here. For example, when a api fetch returns data, modelling the raw data into props to be passed to children
- `this.setState()` is not allowed in this function , it is to be done in componentWillReceiveProps or componentDidUpdate

  1. componentDidUpdate(prevProps,prevState)
代码语言:javascript
复制
- Invoked right after the changes are pushed to the DOM 
- I have used it whenever the required data is not at the first render (waiting for api call to come through) and DOM requires to be changed based on the data received
- Example, based on the age received show the user if he is eligible for application for an event

  1. componentWillUnmount()
代码语言:javascript
复制
- As the official docs mentions, any event listeners or timers used in the component to be cleaned here

在不确定的时刻,我如何知道答案是否在于生命周期方法?

我建议的是什么类比

  1. 组件本身就会触发更改。
代码语言:javascript
复制
- Example, Enable editing of fields on click of an edit button
- A function in the same component changes the state no involvement of lifecycle functions

  1. 更改在组件外部触发。
代码语言:javascript
复制
- Example, api call finished , need to display the received data
- Lifecycle methods for the win.

下面是一些更多的场景-

  1. 状态/道具的更改是否需要修改DOM?
代码语言:javascript
复制
- Example, if the current email is already present , give the input class an error class.
- `componentDidUpdate`

  1. 状态/道具的更改是否需要更新数据?
代码语言:javascript
复制
- Example, parent container which formats data received after api call and passes the formatted data to children.
- `componentWillUpdate`

  1. 传递给孩子的道具被更改,孩子需要更新。
代码语言:javascript
复制
- Example, 
- `shouldComponentUpdate`

  1. 添加事件侦听器
代码语言:javascript
复制
- Example, add a listener to monitor the DOM, based on window size.
- `componentDidMount`
- 'componentWillMount' , to destroy the listner

  1. 调用api
代码语言:javascript
复制
- 'componentDidMount'

来源-

  1. Docs - https://facebook.github.io/react/docs/component-specs.html
  2. 清除生命周期概念的 scotch.io文章
  3. 事件侦听器- https://facebook.github.io/react/tips/dom-event-listeners.html
票数 13
EN

Stack Overflow用户

发布于 2016-08-13 06:58:32

一些最常用的生命周期方法的典型用例:

componentWillMount:在初始呈现之前调用。对于进行AJAX调用很有用。例如,如果您需要获取用户信息来填充视图,那么这是一个很好的方法。如果您有一个AJAX调用,最好在AJAX调用完成之前呈现一个不确定的加载栏。我还使用componentWillMount调用setInterval,并在页面呈现之前禁用Chrome的拖放功能。

componentDidMount:在组件呈现后立即调用。如果需要访问DOM元素,则非常有用。例如,我使用它来禁用复制和粘贴到密码输入字段。如果您想知道组件的状态,那么很适合调试。

componentWillReceiveProps:当组件接收到新的道具时调用。用于使用新的道具设置状态,而不需要重新呈现。

票数 3
EN

Stack Overflow用户

发布于 2016-12-07 14:52:03

componentWillReceiveProps是更新生命周期方法的一部分,在呈现开始之前被调用。最明显的例子是当新的道具被传递给组件时。例如,我们有一个表单组件和一个Person组件。表单组件只有一个,允许用户通过在输入中键入更改名称。输入绑定到onChange事件,并在窗体上设置状态。然后状态值作为支柱传递给Person组件。

代码语言:javascript
复制
import React from 'react';
import Person from './Person';

export default class Form extends React.Component {
  constructor(props) {
    super(props);
    this.state        = { name: '' } ;
    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(event) {
    this.setState({ name: event.currentTarget.value });
  }

  render() {
    return (
      <div>
        <input type="text" onChange={ this.handleChange } />
        <Person name={ this.state.name } />
      </div>
    );
  }
}

每当用户在此中键入内容时,就会开始为Person组件更新。组件上调用的第一个方法是componentWillReceiveProps(nextProps),传入新的支柱值。这使我们能够将传入的道具与当前的道具进行比较,并根据该值做出逻辑决策。我们可以通过调用this.props来获得当前的支持,新的值是传递给方法的nextProps参数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38929991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档