react组件:如何从道具中设置仅限关键点的属性



我正在构建一个简单的react本机组件,它使用自己一个需要attribue的组件,而attribue只是一个键。例如:

export default class MyComponent extends Component {
render() {
return (
<LineChart bezier />
);
}
}

我的组件使用的组件是LineChart,它需要像bezier这样的属性
我不知道如何以动态方式将此属性传递给LineChart,即使用道具。。。我确实尝试过这样的东西:

<LineChart this.props.interpolation />

但这在语法上是无效的。

有线索吗?

"仅钥匙道具";实际上是布尔值。

所以,当你做<Component bezier />时,你只是用一种更短的方式说与<Component bezier={true} />相同的话。

如果你想让它动态,你可以做一些类似的事情

const { bezier, interpolation } = this.props;
return <Component bezier={bezier} interpolation={interpolation} />

PS:考虑到props内部的值将是布尔值。

我使用了函数组件,在该组件中我像一样传递密钥

<LineChart bezier={Your_DATA}/>

最新更新