Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript React Components 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

Chris Adams
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Adams
Front End Web Development Techdegree Graduate 29,378 Points

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.