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

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

Wrong data type being passed to a React PropType.

and I can't for the life of me figure out where.

I'm spending a lot of time trying to revise what I've learned about React and I'm up to this video. Here's what the console says.

Warning: Failed propType: Invalid prop `score` of type `string` supplied to `Counter`, expected `number`. Check the render method of `Player`.

And it returns line 40 in the console for the player Component. Any idea where I've gone wrong? Thanks

//Header component
function Header(props) {
  return (

    <div className="header">
        <h1>{ props.title }</h1>
    </div>

  );
}

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

function Counter(props) {
    return (

        <div className = "counter">
            <button className="counter-action decrement"> - </button>
            <div className="counter-score"> {props.score} </div>
            <button className="counter-action increment"> + </button>
        </div>

    );  
}

Counter.propTypes = {
   score: React.PropTypes.number.isRequired,
};

function Player(props) {

    return (
        <div className="player">
            <div className="player-name">
                {props.name}
            </div>
            <div className="player-score">
                <Counter score="{props.score}" />
            </div>
        </div>
    );              
}

Player.propTypes = {
   name: React.PropTypes.string.isRequired,
   score: React.PropTypes.number.isRequired, 
};

//Application component
function Application(props) {
   return (

    <div className="scoreboard">

        <Header title={props.title} />

            <div className="players">       
                <Player name="Jim Hoskins" score={31}  />
                <Player name="Andrew Chalkey" score={33} />

            </div>  

    </div>  
   );
}



Application.propTypes= {
  title: React.PropTypes.string,
}

Application.defaultProps = {
   title: "Scoreboard",
}

//Render application
ReactDOM.render(
    <Application />, document.getElementById('container')

 );

///* ReactDOM.render(<h1>HELLO</h2>, document.getElementById('container'));*/

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

It think the line in Player where you pass the score to Counter is the issue:

                <Counter score="{props.score}" />

With the quotes you are converting score to a string. To pass the number you should use

                <Counter score={props.score} />
Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

again I have to ask myself how I did not see this. :D But I appreciate your help, Thanks Seth :)