【问题标题】:React — not defined error?React——未定义错误?
【发布时间】:2016-07-18 17:58:49
【问题描述】:

这段代码实际上在我的环境中工作,我实际上是在尝试创建一个小提琴来提出一个不同的问题,但需要先解决这个问题......

为什么控制台说“收藏”没有定义?

FIDDLE

class BtnFav extends React.Component {
  constructor(props) {
    super(props);
    this.state = {favorited: false};
    this.handleClick = this.handleClick.bind(this);
  }
  handleClick() {
    this.setState({favorited: !this.state.favorited});
  }
  render() {
    var favStatus = this.state.favorited ? 'btn-fav' : 'btn-notfav';
    return (
      <button className={favStatus} onClick={this.handleClick}>FAVORITE</button>
    );
  }
};

【问题讨论】:

  • 刚刚检查了你的小提琴它工作得很好。没有任何错误的迹象。

标签: reactjs state


【解决方案1】:

我能找到的唯一错误是“背景”的拼写错误。在您指定的原始示例中:

.btn-fav {
    backround-color: green;
}

.btn-notfav {
    backround: red;
}

该属性正确拼写为backgroundbackground-color

查看此更新示例:https://jsfiddle.net/69z2wepo/49511/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-05
    • 2019-02-27
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多