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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Roderick Hung](https://secure.gravatar.com/avatar/706e0bf001c1c9a394dc1d52aea6e125?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Roderick Hung
Front End Web Development Techdegree Graduate 23,181 PointsI can not get the player id to increment by one. Help please
'''javascript const handleAddPlayer = (name) => { setPlayers(prevPlayers => [ ...prevPlayers, { name, score: 0, id: nextPlayerId.current++ } ]); } '''
3 Answers
![Sail Liao](https://secure.gravatar.com/avatar/b20353db595e09a635d6f7256dd77191?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Sail Liao
6,654 PointsI think it's because the id is based on the output of setPlayers(). State updates are batched, so when you add players really fast, the newId would not change.
I move the increment out of the setPlayers() function by store the nextId into a variable:
const nextPlayerId = playerIdRef.current;
playerIdRef.current++
setPlayers(prevPlayers => [
...prevPlayers,
{
name,
score: 0,
id: nextPlayerId
}
]);
```JavaScript```
![Steven Parker](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsThis code is using the postincrement operator (nextPlayerId.current++
). While this does increment the value in the specified variable, the value of the expression itself is the current value, before the increment occurs.
There's also a preincrement operator (++nextPlayerId.current
) that increments the value first and then the expression itself returns the increased value. You might need to use that one instead.
![Roderick Hung](https://secure.gravatar.com/avatar/706e0bf001c1c9a394dc1d52aea6e125?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Roderick Hung
Front End Web Development Techdegree Graduate 23,181 PointsSteven Parker, the video used nextPlayerId.current++ and was able to increment by one each time. My code is exactly the same but, does not increment by one after a player. How can I use nextPlayerId.current++ and increment by one each time?
![Steven Parker](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsSharing only code snippets can be problematic if the issue is actually being caused elsewhere in the code. When using a workspace, making a snapshot and posting the link to it here can help analysis greatly.