[英]How does the child component receive props from the parent component without listening for the lifecycle method?
To update the props
object in the child components, usually the life cycle method ComponentWillReceiveProps
is used.要更新子组件中的
props
对象,通常使用生命周期方法ComponentWillReceiveProps
。 But I realized that the props
for the child component could be updated without listening for ComponentWillReceiveProps
.但我意识到子组件的
props
可以在不监听ComponentWillReceiveProps
情况下更新。
For example, in the following component named App
, the child component Comp
is able to receive the props
without listening for the life cycle method ComponentWillReceiveProps
.例如,在以下名为
App
的组件中,子组件Comp
能够在不侦听生命周期方法ComponentWillReceiveProps
情况下接收props
。
The main App
component:主要
App
组件:
import React from 'react';
import ReactDOM from 'react-dom';
import {Comp} from "./comp";
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
counter: 0
}
this.incrementCounter = this.incrementCounter.bind(this);
}
componentDidMount() {
this.setState({
counter: 100
})
}
incrementCounter() {
this.setState({
counter: this.state.counter + 1
})
}
render() {
return (
<div>
<button onClick={this.incrementCounter}>Increment</button>
<br />
<br />
<Comp counter={this.state.counter}/>
</div>
)
}
}
ReactDOM.render(<App />, document.getElementById('container'));
and the child Comp
component:和子
Comp
组件:
import React from 'react';
export class Comp extends React.Component {
constructor(props) {
super(props);
this.state = {}
}
componentDidUpdate(prevProps, prevState) {
console.log("prev props ", prevProps);
console.log("prev state", prevState)
}
render() {
return <span>Props received: {JSON.stringify(this.props)}</span>
}
}
Here is also the working demo of the above code I prepared这也是我准备的上述代码的工作演示
You will see that the child component Comp
receives property counter
without listening for any lifecycle method.您将看到子组件
Comp
接收属性counter
而无需侦听任何生命周期方法。
What does it mean?这是什么意思? Am I missing anything?
我错过了什么吗?
componentWillReceiveProps()
is a deprecated life-cycle method that is triggered when a component is about to receive new props. componentWillReceiveProps()
是一个不推荐使用的生命周期方法,当组件即将接收新的 props 时会触发它。 However, it has no control over whether the component actually receives those props.但是,它无法控制组件是否实际接收这些道具。
https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops
The component will receive updated props from parent regardless of whether componentWillReceiveProps()
is executed.无论是否执行
componentWillReceiveProps()
,组件都会从父级接收更新的道具。
import React from 'react';
export class Comp extends React.Component {
constructor(props) {
super(props);
this.state = {}
}
render() {
console.log(this.props.counter) //still gets printed with new props data
return <span>Props received: {JSON.stringify(this.props)}</span>
}
}
Note that props are passed down strictly from Parent to Child Component.请注意,道具严格从父组件传递到子组件。
When a Parent component is re-rendered via state-change, it re-renders the Child components as well and it passes down updated props.当父组件通过状态更改重新渲染时,它也会重新渲染子组件并传递更新的道具。 Now the children component have access to those new props and they go through the React life-cycle of events.
现在子组件可以访问这些新的 props,它们会经历 React 事件的生命周期。
Events like componentDidUpdate()
and componentWillReceiveProps()
allow you to execute some extra logic, but they are not required for the component to receive props or re-render.像
componentDidUpdate()
和componentWillReceiveProps()
这样的事件允许你执行一些额外的逻辑,但它们不是组件接收道具或重新渲染所必需的。 The component will re-render regardless of their usage.无论使用情况如何,组件都将重新渲染。
Additionally, a Child-component can update itself via internal state-updating the same way any class-based component would.此外,子组件可以通过内部状态更新来更新自身,就像任何基于类的组件一样。
Every time you do setState on the parent node/component it re-renders all the child component with the latest props.每次在父节点/组件上执行 setState 时,它都会使用最新的 props 重新渲染所有子组件。 So basically whenever parent component is re-rendered, all the child components of that node are re-rendered.
所以基本上每当父组件被重新渲染时,该节点的所有子组件都会被重新渲染。
The default behavior is to re-render on every state change, and in the vast majority of cases you should rely on the default behavior.默认行为是在每次状态更改时重新渲染,在绝大多数情况下,您应该依赖默认行为。
In case you don't want your child component to re-render on some condition the you can use shouldComponentUpdate lifecycle.如果您不希望您的子组件在某些情况下重新渲染,您可以使用 shouldComponentUpdate 生命周期。 This lifecycle is just a performance optimization.
这个生命周期只是一个性能优化。 Read about shouldcomponentupdate
阅读 shouldcomponentupdate
Also consider using PureComponent instead of component.还可以考虑使用 PureComponent 而不是组件。 It performs a shallow comparison of props and state, and reduces the chance that you'll skip a necessary update.
它对 props 和 state 进行浅层比较,并减少您跳过必要更新的机会。 Read about PureComponent
阅读 PureComponent
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.