Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript React Components (2018) Managing State and Data Flow Communicating Between Components

cody gamble
cody gamble
13,312 Points

I am getting this error TypeError: props.changeScore is not a function

There has not been a clear answer on any of the previous questions with the same name,

import React from 'react';

const Counter = (props) => {

return (
  <div className="counter">
    <button className="counter-action decrement" onClick={() => props.changeScore(-1)}> - </button>
    <span className="counter-score">{ props.score }</span>
    <button className="counter-action increment" onClick={() => props.changeScore(1)}> + </button>
  </div>
);

}

export default Counter;

cody gamble
cody gamble
13,312 Points

One discussion OP said he found an error in his App.js, but he never mentioned what it was

import React, { Component } from 'react';
import Header from './Header';
import Player from './Player';

class App extends Component {
  state = {
    players: [
      {
        name: "Cody",
        score: 0,
        id: 1
      },
      {
        name: "Max",
        score: 0,
        id: 2
      },
      {
        name: "Sam",
        score: 0,
        id: 3
      },
      {
        name: "Zoe",
        score: 0,
        id: 4
      }
    ]
  };

  handleScoreChange = (delta) => {
    // this.setState( prevState => ({
    //   score: prevState.score + 1
    // }));
    console.log(delta);
  }

  handleRemovePlayer = (id) => {
    this.setState( prevState => {
      return {
        players: prevState.players.filter(p => p.id !== id)
      };
    });
  }

  render() {
    return (
      <div className="scoreboard">
        <Header 
          title="Scoreboard" 
          totalPlayers={this.state.players.length} 
        />

        {/* Players list */}
        {this.state.players.map( player =>
          <Player 
            name={player.name}
            score={player.score}
            id={player.id}
            key={player.id.toString()} 
            ChangeScore={this.handleScoreChange}
            removePlayer={this.handleRemovePlayer}           
          />
        )}
      </div>
    );
  }
}

export default App;

2 Answers

cody gamble
cody gamble
13,312 Points

Ah HA! I found my problem

        {/* Players list */}
        {this.state.players.map( player =>
          <Player 
            name={player.name}
            score={player.score}
            id={player.id}
            key={player.id.toString()} 
            changeScore={this.handleScoreChange}
            removePlayer={this.handleRemovePlayer}           
          />
        )}
      </div>
    );
  }
}

the changeScore prop in the player component I had capitalized the first letter by accident

I encountered a similar issue, but like the OP, I had also included a typo in my code. The video runs quite fast, so I would recommend that anyone following along takes the time to make sure all their code matches what Guil writes. I've slowed the video down quite a bit now.