我厌倦了一直这样做:
<Elem x={x} y={y} z={z} />
<Elem x={this.props.x} y={this.props.y} z={this.props.z} />
Run Code Online (Sandbox Code Playgroud)
有没有办法可以让这样的东西起作用?
<Elem x, y, z />
Run Code Online (Sandbox Code Playgroud)
要么
<Elem {x, y, z} />
Run Code Online (Sandbox Code Playgroud)
小智 33
如果您的变量包含在对象中,例如this.props,您传播对象:
<Elem {...this.props} />
Run Code Online (Sandbox Code Playgroud)
否则,您传播包含所需变量的新对象:
<Elem {...{ x, y, z }} />
Run Code Online (Sandbox Code Playgroud)
应该注意的是,这仅适用于对象。例如:
this.props = {
x: 'foo',
y: 'bar',
z: 'baz',
}
const {
x,
...allOtherProps
} = this.props
<Elem { ...allOtherProps } /> // works (allOtherProps is an object)
<Elem { ...x } /> // does not work (x is not an object)
<Elem x={ x } /> // works
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6679 次 |
| 最近记录: |