JavaScript React Basics (retired) Designing Data Flow Communicating Events

Gbemi Ojo
Gbemi Ojo
735 Points

this.bind on callback is producing an error

I'm having trouble using the this.bind assignment in my code. The course is clearly out of date and I've had to do some refactoring using external resources, but now I'm having some trouble figuring out what to substitute for this.bind on the callback function in the onScoreChange prop call. Whenever I add it in, I get an "Uncaught syntax error", and without it, I get an uncaught reference error for Index.

Here's all my current code. It's written in es6.

const Players = [
    {
        name: "Blake Shelton",
        score: 56,
        id: 1,
    },
    {
        name: "Van Jones",
        score: 34,
        id: 2,
    },
    {
        name: "Anderson Cooper",
        score: 52,
        id: 3,
    },
    {
        name: "Jason Vorhees",
        score: 29,
        id: 4,
    },
];

let Header = (props) => {
    return (
        <div className="header">
            <h1> {props.title}</h1>
        </div>
    )
}

Header.propTypes = {
    title: PropTypes.string.isRequired,
};

class Application extends React.Component {
    static propTypes = {
        title: PropTypes.string,
        source: PropTypes.arrayOf(PropTypes.shape({
        name: PropTypes.string.isRequired,
        score: PropTypes.number.isRequired,
        id: PropTypes.number.isRequired
        })).isRequired
    }
    static defaultProps =  {
        title: "Scoreboard"
    }
    state = {
        players: this.props.source,
    }

    onScoreChange = (index, delta) => {
            console.log('onScoreChange', index, delta);
            this.state.players[index].score += delta;
            this.setState(this.state);
        }

    render() {
        return (
            <div className="scoreboard">
        <Header title={this.props.title}/>
        <div className="players"> 
            {this.state.players.map((player)=> {
                return (
                    <Player 
                        onScoreChange={(delta)=> {this.onScoreChange(index, delta)}}
                        name={player.name} 
                        score={player.score} 
                        key={player.id}/>
                );
                })}
        </div>
        </div>
        )
    }
}

let Counter = (props) => 
    <div className="counter"> 
        <button className="counter-action decrement" onClick={()=> props.onChange(-1)}> </button>
        <div className="counter-score"> {props.score} </div>
        <button className="counter-action increment" onClick={()=> props.onChange(1)}> </button>
    </div>

Counter.propTypes = {
    score: PropTypes.number.isRequired,
    onChange: PropTypes.func.isRequired,
}

let Player = (props) => 
    <div className="player">
        <div className="player-name"> {props.name} </div>
        <div className="player-score"> 
        <Counter score={props.score} onChange={props.onScoreChange}/>   
        </div>
    </div>

Player.propTypes = {
    name: PropTypes.string.isRequired,
    score: PropTypes.number.isRequired,
    onScoreChange: PropTypes.func.isRequired,
}

ReactDOM.render(<Application source={Players}/>, document.getElementById('root'));

1 Answer

Stuart Wright
Stuart Wright
41,092 Points

I haven't tested this but one way I often see this problem solved is by binding this inside the component's constructor method. Something like this:

class Application extends React.Component {
  constructor(props) {
    super(props);
    this.onScoreChange = this.onScoreChange.bind(this);
  }
  // The rest of your Application code here
}
Gbemi Ojo
Gbemi Ojo
735 Points

Thanks a lot for your help Stuart!