Below is code snippet
const MuiSelect = () => {
const [country , setCountry] = useState('')
console.log("C1: " + country)
const handleChange = (event : React.ChangeEvent<HTMLInputElement>) => {
setCountry(event.target.value as string)
console.log("C2: " + country);
}
return (
<Box width="250px">
<TextField label="Select country"
select
value={country}
onChange={handleChange}
fullWidth>
<MenuItem value="PK">Pakistan</MenuItem>
<MenuItem value="US">United States</MenuItem>
<MenuItem value="SA">Saudi Arabia</MenuItem>
<MenuItem value="IR">Iran</MenuItem>
</TextField>
</Box>
)
}
In C1
I am getting updated value when component render/re-render. But in C2
I am getting old/previous state of the country. I am having difficult time to understand this. Can someone explain?