React: “this” is undefined inside a component function

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

    I ran into a similar bind in a render function and ended up passing the context of this in the following way:

    {someList.map(function(listItem) {
      // your code
    }, this)}
    

    I've also used:

    {someList.map((listItem, index) =>
        
    )}

提交回复
热议问题