React: “this” is undefined inside a component function

前端 未结 10 2116
春和景丽
春和景丽 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:35

    ES6 React.Component doesn't auto bind methods to itself. You need to bind them yourself in constructor. Like this:

    constructor (props){
      super(props);
      
      this.state = {
          loopActive: false,
          shuffleActive: false,
        };
      
      this.onToggleLoop = this.onToggleLoop.bind(this);
    
    }
    

提交回复
热议问题