【问题标题】:React's behaviour when pass through props are named the same as other propsReact 在通过 props 时的行为与其他 props 命名相同
【发布时间】:2020-06-10 22:41:23
【问题描述】:

假设我在 React 中有以下内容:

const Component = () => {
  const props = { one: 1, two: 2, three: 3 };

  return <AnotherComponent {...props} one="1" />;
}
  1. oneAnotherComponent 接收时,props 有什么价值?
  2. 通过道具与其他道具的顺序是否重要?

我有一个我认为是由此引起的错误,我找不到涵盖这种极端情况的资源。

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    one 的值将是字符串 "1"

    是的,顺序很重要。之后的 props 会覆盖之前的同名 props。

    【讨论】:

      【解决方案2】:

      像这样:

      return <AnotherComponent {...{...props, one: 1}} />;
      

      【讨论】:

        猜你喜欢
        • 2021-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-30
        • 1970-01-01
        • 1970-01-01
        • 2019-01-04
        • 1970-01-01
        相关资源
        最近更新 更多