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)
![Abhimanu Saharan](https://secure.gravatar.com/avatar/2311cd44b2b4d0b4c20a9da3d588fcff?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Abhimanu Saharan
Front End Web Development Techdegree Student 36 PointsuseState hook setting different value than what is passed into setState()
I have a select menu with some options. When I change the option in select menu I am setting the state of currentValue using setNewValue(event.target.value) and logging the new value of variable to the console.
However the value set for the variable is different from the value I am passing into setNewValue.
const classGrades = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let [classGrade, setClassGrade] = useState(1);
let classSelect = () => {
return (
<div>
<select value={classGrade} onChange={(e) => {
setClassGrade(e.target.value)
console.log(`I selected ${e.target.value}, so classgrade is now ${classGrade}`)
}}>
{classGrades.map(g => <option key={g} value={g}>{g}</option>)}
</select>
</div>
)
}
1 Answer
![Clayton Perszyk](https://secure.gravatar.com/avatar/91570a217e3dbbeb3a4bb32bd92c70e8?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Clayton Perszyk
Treehouse Moderator 48,838 PointsLooks like the issue is this:
useStateis an asynchronous hook and it doesn’t change the state immediately, it has to wait for the component to re-render.
see this article for more info: https://javascript.plainenglish.io/why-you-shouldnt-always-use-usestate-658994693018#:~:text=TL%3BDR%3A%20useState%20is%20an,t%20trigger%20a%20re%2Drender.