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 trialMostafa Mostafa
167 Pointsi have a problem in increment-ting need Help!!!
in my increment when i click the button to add plus one it laterally add one as concatenation ???
<script type="text/babel">
var Users=[
{
name:"Mostfa",
score:"21",
id:1
},
{
name:"Alicja",
score:"40",
id:2
},
{
name:"Mazen",
score:"34",
id:3
},
];
function Header(props){
return (
<div className="header">
<h1>{props.title}</h1>
</div>
);
}
Header.propTypes = {
title:PropTypes.string.isRequired
};
class Counter extends React.Component
{
constructor (props) {
super(props);
this.state = { score:props.step };
this.incrementScore=this.incrementScore.bind(this)
this.decrementScore=this.decrementScore.bind(this)
}
incrementScore(){
this.setState( {
score: this.state.score +1,
})};
decrementScore(){
this.setState({
score:this.state.score -1
})
};
render (){
return(
<div className="counter">
<button className="counter-action decrement" onClick={this.decrementScore}>-</button>
<div className="counter-score">{this.state.score} </div>
<button className="counter-action increment" onClick={this.incrementScore}>+</button>
</div>
);
}
}
Counter.propTypes = {
step: PropTypes.number.isRequired, };
function Players(props) {
return(
<div className="player">
<div className="player-name">
{props.name}
</div>
<div className="player-score">
<Counter step={props.score} />
</div>
</div>
);
}
Players.proptypes ={
name:PropTypes.string,
score:PropTypes.string,
};
function Play(props){
return (
<div className="scoreboard">
<Header title={props.title} />
<div className="players">
{props.players.map((players)=>{
return <Players name={players.name} score={players.score} id={players.id} />
})}
</div>
</div>
);
}
Play.proptypes ={
title:PropTypes.string,
players: PropTypes.arrayOf(
PropTypes.shape({
name:PropTypes.string,
score:PropTypes.string,
id:PropTypes.number,
}))
};
Play.defaultProps = {
title:"scoreboared",
}
ReactDOM.render(<Play players={Users}/>, document.getElementById('container'));
</script>
</body>
</html>
1 Answer
Ari Misha
19,323 PointsHello there! You're facing the issue 'coz you passed score as a string value. Hence, string concatenation. (:
~ Ari
Mostafa Mostafa
167 PointsThanks ,man you the best :)
Xayaseth Boudsady
21,951 PointsXayaseth Boudsady
21,951 PointsI'm glad Ari was able to help, I couldn't follow along with this formatting...