繁体   English   中英

如何在div React Component jsx内调用函数

[英]How to call a function inside the div React Component jsx

这是Component构造函数,我已经绑定了checkActive函数,该函数将父组件的props与参数进行比较。

constructor(props) {
    super(props);

    this.checkActive = this.checkActive.bind(this);
  }

  checkActive(option) {
    if (this.props.active === option) {
      return 'active';
    }
  }

我想对我的一个div进行“激活”。 所以我开始:

<div {this.checkActive('meet')}>

我想将其渲染为: <div active>

错误显示: 意外的令牌,预期的...

我将函数弄错了吗,或者我们无法在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.

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