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 Adding Items to State

is there a reason why we don't use preState inside the AddPlayerForm but we use it everywhere else in the app??

is there a reason why we don't use preState inside the AddPlayerForm but we use it everywhere else in the app??

Is it because we are not performing a calculation based on a previous state?

when is it ideal to use prevState and when is not??

I was asking myself the same thing because we should be treating State is if it were immutable.

This might be ugly but it works for me:

handleAddPlayer = ( name) => {

    const newIndex = this.state.players.length +1;

    this.setState( prevState => {
      return {
        players: [ ...prevState.players,
          {
            name,
            score: 0,
            id: newIndex
          }
        ]
      };
    });
  }

1 Answer

yes. I kept watching and saw that they fix it in the next video. i had a lot of troubles on Redux section. the course files have a lot of broken old dependencies. was hell to get it to work.