[英]Class names for dynamic children React
I have a set of buttons created from an array, however I'm unsure how to set individual classNames for them. 我有一组从数组创建的按钮,但是我不确定如何为它们设置单独的className。 Is there an easy way to this? 有一个简单的方法吗?
var ButtonContainer = React.createClass({
render: function(){
var answerList = this.props.answerList.map(function(input, i){
return <SingleButton key={'button'+i} singleAnswer={input}/>
}.bind(this));
return <div> {answerList} </div>
}
})
var SingleButton = React.createClass({
render: function(){
return (
<div>
<button className='quiz-button'>{this.props.singleAnswer}</button>
</div>
)
}
});
I've tried className={this.props.key} but that doesn't seem to work. 我已经尝试过className = {this.props.key},但这似乎不起作用。 Thanks for any help! 谢谢你的帮助!
Since React v0.12 key
and ref
are removed from props
: 由于React v0.12 key
和ref
从props
中删除 :
You can no longer access this.props.ref and this.props.key from inside the Component instance itself. 您不再可以从Component实例本身内部访问this.props.ref和this.props.key。 So you need to use a different name for those props. 因此,您需要为这些道具使用其他名称。
That is why setting className={this.props.key}
wont work. 这就是为什么设置className={this.props.key}
原因。 But you can try this: 但是您可以尝试以下操作:
return <SingleButton key={'button'+i} className={'button'+i} singleAnswer={input}/>
and then 接着
<button className={this.props.className}>{this.props.singleAnswer}</button>
Related question: This.key in React.js 0.12 相关问题: React.js 0.12中的This.key
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.