简体   繁体   English

在 React 中,将所有 props 从父组件传递给子组件是一个好习惯吗?

[英]In React, is it a good practice to pass all the props from parent to child component?

Say I have a parent component and one child component.假设我有一个父组件和一个子组件。

The props has ten keys, but child needs only three. props具有十把钥匙,但孩子只需要三把。

What is the better way to pass props to child component?将道具传递给子组件的更好方法是什么?

class Parent extends Component {
  render() {
     { /*pass all the ten keys into child*/ }
     <Child {...this.props} />
  }
}

or或者

class Parent extends Component {
  render() {
     { /*pass only the needed keys into child*/ }
     <Child key1={key1} key2={key2} key3={key3}/>
  }
}

Passing all the props generally isn't a good idea.传递所有道具通常不是一个好主意。 More props means more things that will cause the child component to unnecessarily re-render.更多的道具意味着更多的东西会导致子组件不必要地重新渲染。 However, it can be convenient to use the spread operator on a subset of those props.但是,在这些 props 的子集上使用扩展运算符很方便。 For example, a parent component may receive a lot of props, but doesn't use most of them and instead hands all but one or two off to a child component.例如,一个父组件可能会收到很多 props,但不会使用其中的大部分,而是将除了一两个之外的所有道具交给子组件。 Consider this example using something like redux-form:考虑这个使用类似 redux-form 的例子:

export function Form({ handleSubmit, ...rest }) {
  return (
    <form onSubmit={handleSubmit}>
      <Field name="name" component={FormInput} />  
      <SaveButton {...rest} />
    </form>
  );
}

The outer form component only cares about the submit function.外部表单组件只关心提交功能。 Other props indicating whether the form is dirty, valid, etc, would be used by the <SaveButton /> to determine whether or not to disable the button. <SaveButton />将使用其他指示表单是否脏、有效等的道具来确定是否禁用按钮。

This is convenient because we avoid having to declare props for a component that doesn't use them.这很方便,因为我们不必为不使用它们的组件声明 props。 We just pass them through.我们只是通过它们。 But as stated previously, use this pattern with caution, making sure you're aware of which props you're actually handing down, because it could cause performance issues or even side effects.但如前所述,请谨慎使用此模式,确保您知道实际传递的是哪些道具,因为它可能会导致性能问题甚至副作用。

In fact, I'd go so far as to say that if you find yourself passing props down through a hierarchy frequently, you probably have a design problem and should be leveraging your redux store more efficiently.事实上,我什至要说,如果您发现自己经常在层次结构中向下传递 props,那么您可能遇到了设计问题,应该更有效地利用您的 redux 存储。

No, it's a bad idea usually.不,这通常是个坏主意。 In general, it's better to just pass the component what it needs:一般来说,最好只传递组件所需的内容:

  1. Fewer props needed to determine if a component has changed确定组件是否已更改所需的道具更少
  2. Easier to understand what a component does更容易理解组件的作用
  3. You can statically analyze what a component needs and refactoring becomes easier您可以静态分析组件需要什么,重构变得更容易

You can use lodash's _.pick function:你可以使用 lodash 的_.pick函数:

<Child {...(_.pick(this.props, ['key1', 'key2', ...]))} />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM