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) React Component Patterns Optimize Performance with PureComponent

I think I write the same code as Guil, but all the players still being re-rendered everytime I clicked the counterbutton

code in Player.jsx

import React,{PureComponent} from "react";
import Counter from "./Counter";


class Player extends PureComponent {
  render() {
    console.log(this.props.name)
    return (  
    <div className="player">
    <span className="player-name">
    <button onClick={()=> {this.props.removePlayer(this.props.id)}} className="remove-player">✖</button>
    {this.props.name}
    </span>
    <Counter startPoint={this.props.startPoint} handleScore={this.props.handleScore} index={this.props.index}/>
  </div>
  );
  }
}

1 Answer

Tomas Schaffer
Tomas Schaffer
11,606 Points

You dont setting props to class Player so your component cant read props from parrent component even if you are passing it. And you are tying also execute function from parrent component. Then you are passing this props to child component Counter which is also undifened.

class Player extends Component { constructor(props) { super(props); } render() { console.log(this.props.name) return (
<div className="player"> <span className="player-name"> <button onClick={()=> {this.props.removePlayer(this.props.id)}} className="remove-player">✖</button> {this.props.name} </span> <Counter startPoint={this.props.startPoint} handleScore={this.props.handleScore} index={this.props.index}/> </div> ); } }