这个React组件中的{... props}是什么意思?

Sna*_*ail 3 reactjs react-router

刚开始使用react-router.

当我在github(底部)遇到这段代码时,我正在使用react-router @ next(版本4 ).我有弱的React + ES6-fu因此需要你的帮助.

  1. 这里的{... props}是指向组件发送道具吗?
  2. {... props}在这里如何影响custom ="prop"?

ž

<Match pattern="/foo" 
       render={(props) => ( 
         <YourRouteComponent {...props} custom="prop"/> 
       )} 
/>
Run Code Online (Sandbox Code Playgroud)

Yad*_*ran 8

考虑下面的例子:

props = {
    propA: "valueA",
    propB: "valueB",
    propC: "valueC"
};
Run Code Online (Sandbox Code Playgroud)

然后,

<SomeComponent {...props} />
Run Code Online (Sandbox Code Playgroud)

相当于

<SomeComponent propA="valueA" propB="valueB" propC="valueC" />
Run Code Online (Sandbox Code Playgroud)
<SomeComponent {...props} propC="someOtherValue" />
Run Code Online (Sandbox Code Playgroud)

相当于

<SomeComponent propA="valueA" propB="valueB" propC="someOtherValue" />
Run Code Online (Sandbox Code Playgroud)

另请注意

<SomeComponent propC="someOtherValue" {...props} />
Run Code Online (Sandbox Code Playgroud)

会变成

<SomeComponent propA="valueA" propB="valueB" propC="valueC" />
Run Code Online (Sandbox Code Playgroud)

顺序是非常重要的.

阅读有关JSX Spread Operator的更多信息...