Answer the question
In order to leave comments, you need to log in
How to get value from promise?
I make a request to the server https://reqres.in/api/login
My state:
"email": "[email protected]",
"password": "cityslicka"
"token":""
fetch("https://reqres.in/api/login", {
method: "POST",
body: JSON.stringify({
email: state.email,
password: state.password,
}),
headers: { "Content-Type": "application/json" },
})
.then((response) => {
if (response.status !== 200) {
return Promise.reject();
}
return response.json();
})
.then(res => state.token = res)
Answer the question
In order to leave comments, you need to log in
https://ru.reactjs.org/docs/hooks-intro.html
Familiarize yourself with the basics of React, namely the useState hook
Error in the correctness of the state change (change by reference does not work)
The last then works great
fetch("https://baconipsum.com/api/?type=meat-and-filler", )
.then((response) => {
if (response.status !== 200) {
return Promise.reject();
}
return response.json();
})
.then(res => console.log("res", res))
state.token = res
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question