简体   繁体   English

子组件如何在不监听生命周期方法的情况下从父组件接收 props?

[英]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这也是我准备的上述代码的工作演示

编辑 jovial-engelbart-3dsun

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.

相关问题 如何将道具从父组件添加到子组件的子组件 - How to add props from parent component to child component of child component 如何在不使用道具的情况下将数据从父组件传递到子组件 - How to pass data from parent to child component without using props 如何将道具从父组件渲染到子组件? - How to render props from parent component to child? 如何在子组件中从父组件重置道具? - How do I reset props from a parent component in a child component? 当我通过 props 从父组件将变量的值传递给子组件时,我收到一个未定义的值 - I receive an undefined value when I pass the value of a variable through props from a parent component to a child component 如何将道具从子组件传递到父组件到 ReactJS 中的另一个子组件? - How to pass props from child component to parent component to another child component in ReactJS? 聚合物1:父组件未从子组件的“分离”方法侦听火灾事件? - Polymer 1: parent component not listening to a fire event from a child component's 'detached' method? React Native子组件未从父组件接收更新状态 - React Native child component does not receive updated state from parent React hook state 在父组件中更新,但子组件没有收到新的道具 - React hook state updated in parent but child component does not receive new props 没有从子组件中调用无参数props方法 - no argument props method does not get called from child component in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM