componentWillReceiveProps和其他生命周期方法似乎具有欺骗性的诱惑力,将不必要的复杂性和噪声引入到缺乏经验的反应性编码器手中。它们为什么会存在?他们最典型的用例是什么?在不确定的时刻,我如何知道答案是否在于生命周期方法?
发布于 2016-08-13 10:50:12
我已经使用react几个月了,我的大部分工作都是从零开始创建一个大型应用程序。因此,同样的问题在一开始就出现了。
下面的信息是基于开发过程中的学习和通过多个文档来获得正确的信息。
正如问题中所问到的,下面是react中生命周期方法的几个用例。
componentWillMount()- 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
componentDidMount()- 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.
componentWillReceiveProps(nextProps,nextState)- 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.
shouldComponentUpdate(nextProps, nextState)- 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
componentWillUpdate(nextProps,nextState)- 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
componentDidUpdate(prevProps,prevState)- 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
componentWillUnmount()- As the official docs mentions, any event listeners or timers used in the component to be cleaned here
在不确定的时刻,我如何知道答案是否在于生命周期方法?
我建议的是什么类比
- 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
- Example, api call finished , need to display the received data
- Lifecycle methods for the win.
下面是一些更多的场景-
- Example, if the current email is already present , give the input class an error class.
- `componentDidUpdate`
- Example, parent container which formats data received after api call and passes the formatted data to children.
- `componentWillUpdate`
- Example,
- `shouldComponentUpdate`
- Example, add a listener to monitor the DOM, based on window size.
- `componentDidMount`
- 'componentWillMount' , to destroy the listner
- 'componentDidMount'
来源-
发布于 2016-08-13 06:58:32
一些最常用的生命周期方法的典型用例:
componentWillMount:在初始呈现之前调用。对于进行AJAX调用很有用。例如,如果您需要获取用户信息来填充视图,那么这是一个很好的方法。如果您有一个AJAX调用,最好在AJAX调用完成之前呈现一个不确定的加载栏。我还使用componentWillMount调用setInterval,并在页面呈现之前禁用Chrome的拖放功能。
componentDidMount:在组件呈现后立即调用。如果需要访问DOM元素,则非常有用。例如,我使用它来禁用复制和粘贴到密码输入字段。如果您想知道组件的状态,那么很适合调试。
componentWillReceiveProps:当组件接收到新的道具时调用。用于使用新的道具设置状态,而不需要重新呈现。
发布于 2016-12-07 14:52:03
componentWillReceiveProps是更新生命周期方法的一部分,在呈现开始之前被调用。最明显的例子是当新的道具被传递给组件时。例如,我们有一个表单组件和一个Person组件。表单组件只有一个,允许用户通过在输入中键入更改名称。输入绑定到onChange事件,并在窗体上设置状态。然后状态值作为支柱传递给Person组件。
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参数。
https://stackoverflow.com/questions/38929991
复制相似问题