[英]How to call a function inside the div React Component jsx
This is the Component constructor and I've already bind the function checkActive which compares the props from parent component with the argument. 这是Component构造函数,我已经绑定了checkActive函数,该函数将父组件的props与参数进行比较。
constructor(props) {
super(props);
this.checkActive = this.checkActive.bind(this);
}
checkActive(option) {
if (this.props.active === option) {
return 'active';
}
}
I would like to render 'active' to one of my div. 我想对我的一个div进行“激活”。 So I start with:
所以我开始:
<div {this.checkActive('meet')}>
I want to render it to : <div active>
我想将其渲染为:
<div active>
The error shows: Unexpected token, expected ... 错误显示: 意外的令牌,预期的...
Did I make the function wrong or we cannot pass the function inside the div? 我将函数弄错了吗,或者我们无法在div内部传递函数?
<div active={this.props.active === "meet" ? "true" : void 0} />
您可以在div
使用className
激活下面的组件
<div className={this.props.active === "meet" ? "active" : ""} />
我想你可以这样尝试
<div className={this.props.active == "meet"?"active":""} />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.