i try to insert some data in my state, but when i try, i've got an error on user_email: "test@mail.com" : The type cast expression is expected to be wrapped with parenthesis
But i don't understand where am i supposed to put parenthesis. This is the code :
const [userLogin, setUserLogin] = useState({
user_email: "",
user_password: "",
});
const login = async (e) => {
e.preventDefault();
console.log(refConnexionMail.current.value);
setUserLogin(
...userLogin,
user_email: 'test@mail.com'
)
await POST(ENDPOINTS.USER_LOGIN, userLogin);
// await GET(ENDPOINTS.USER_LOGIN)
fetch("http://localhost:4200/api/auth/login")
.then((response) => response.json())
.then((data) => {
console.log(data);
});
};
Then, i will replace "test@mail.com" by "refConnexionMail.current.value" which is the data i want to put in my state.
In your code, you are using the spread operator You have to put the parenthesis here
setUserLogin({
...userLogin,
user_email: 'test@mail.com'
})
The type cast expression is expected to be wrapped with parenthesis.
It has to have parenthesis here:
setUserLogin({
...userLogin,
user_email: 'test@mail.com'
})
setState being an async operation, you may or may not have the right value for your POST method. So better do something like this.
setUserLogin((prevState) => {
return {
...prevState,
user_email: 'test@mail.com'
}
})