I want the alert to display lineaData.name="new Name" and lineaData.age=33 when the button is clicked, but it keeps displaying the initial values. What wrong?
I'am using React 16.13.1
Thanks in advance.
const TestComp = () => {
const [lineaData, setLineaData] = useState({
name: "",
age: 0,
});
const [sendData, setSendData] = useState(null);
const handleClick = () => {
setSendData(true);
alert(lineaData.name + " - " + lineaData.age);
};
useEffect(() => {
if (sendData) {
setLineaData({
...lineaData,
name: "new Name",
age: 33,
});
}
}, [sendData]);
return (
<Grid>
<FormControl style={{ width: "335px" }}>
<FormControl>
<TextField
variant="outlined"
type="text"
label="Name:"
name="name"
onChange={(e) =>
setLineaData({
...lineaData,
name: e.target.value,
})
}
value={lineaData.name || ""}
/>
<FormHelperText>{lineaData.nameError}</FormHelperText>
</FormControl>
</FormControl>
<Button type="submit" onClick={handleClick}>
<img src="./imagenes/ok.png" align="right|middle" height="30" alt="" />
</Button>
</Grid>
);
};