简体   繁体   English

反应动态地在组件上注入属性

[英]react dynamically inject a property on a component

having a problem figuring this out.解决这个问题时遇到问题。 If I had a component such as如果我有一个组件,例如

<Stack horizontal>..whatever...</Stack>

How could I make the horizontal attribute be inline kind of like我怎样才能使水平属性像内联一样

<Stack {this.horizontal ? 'horizontal' : 'vertical'>..whatever..</Stack>

In this scenario, it isn't adding a parameter such as className={this.class}, it is just the attribute itself.在这种情况下,它没有添加诸如 className={this.class} 之类的参数,它只是属性本身。

Thanks.谢谢。

If you want to pass an attribute just use props:如果你想传递一个属性,只需使用道具:

const myValue = this.horizontal ? 'horizontal' : 'vertical'
<Stack {myValue}>..whatever..</Stack>

If that doesn't work maybe you can even do:如果这不起作用,也许你甚至可以这样做:

if (this.horizontal) {
   comp = <Stack horizontal>..whatever..</Stack>
} else {
 comp = <Stack vertical>..whatever..</Stack>
}

And then maybe return然后也许会回来

{comp} 

in the render?在渲染中?

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

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