React: “this” is undefined inside a component function

前端 未结 10 2227
春和景丽
春和景丽 2020-11-22 04:59
class PlayerControls extends React.Component {
  constructor(props) {
    super(props)

    this.state = {
      loopActive: false,
      shuffleActive: false,
    }         


        
10条回答
  •  逝去的感伤
    2020-11-22 05:51

    You can rewrite how your onToggleLoop method is called from your render() method.

    render() {
        var shuffleClassName = this.state.toggleActive ? "player-control-icon active" : "player-control-icon"
    
    return (
      
    this.onToggleLoop(event)} spin={this.state.loopActive} />
    ); }

    The React documentation shows this pattern in making calls to functions from expressions in attributes.

提交回复
热议问题