简体   繁体   English

反应组件:如何从道具中设置仅键属性?

[英]react component: how to set a key only attribute from props?

I am building a simple react-native component, which uses itself a component which requires an attribue which is only a key.我正在构建一个简单的 react-native 组件,它使用自己的组件,该组件需要一个属性,这只是一个键。 For example:例如:

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

The component my component uses is LineChart , which expects an attribute like bezier .我的组件使用的组件是LineChart ,它需要像bezier这样的属性。
I don't know how I could pass this attribute to LineChart in a dynamic way, ie using a prop... I did try something like:我不知道如何以动态方式将此属性传递给LineChart ,即使用道具...我确实尝试过类似的方法:

<LineChart this.props.interpolation />

But this is not syntactically valid.但这在语法上是无效的。

Any clue?有什么线索吗?

"key only props" are actually booleans. “仅关键道具”实际上是布尔值。

So when you do <Component bezier /> you are just saying the same as <Component bezier={true} /> in a shorter way.因此,当您执行<Component bezier />时,您只是以更短的方式说与<Component bezier={true} />相同。

If you want to have it dynamic, you can do something like如果你想让它动态,你可以做类似的事情

const { bezier, interpolation } = this.props;

return <Component bezier={bezier} interpolation={interpolation} />

PS: considering that the values inside props will be booleans. PS:考虑到道具内部的值将是布尔值。

I have used functional component in which i pass key like that我使用了功能组件,在其中我像这样传递密钥

<LineChart bezier={Your_DATA}/>

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

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