Answer the question
In order to leave comments, you need to log in
The state does not change, what's wrong?
It is necessary that when the page is loaded, it receives data from the server and saves it to the state, the problem is that the state does not change. Here is a piece of code.
const { loading, error, request } = useHttp();
const [items, setItems] = useState([]);
let getItems = () => {
try {
const data = request("/api/item", "GET").then((answer) => {
setItems(JSON.parse(answer));
});
return JSON parse(data);
} catch (error) {
console.log(error.message);
}
};
useEffect(() => {
getItems();
new WOW.WOW().init();
return () => {};
}, []);
Answer the question
In order to leave comments, you need to log in
You are going to synchronously take the data and change the state before the request is completed. For the same reason, try catch won't work either.
Even if you delete the request to the server and write setItem("test") in useeffect, the state still does not change.
lay out the complete component with the useHttp() hook, and you will be happy
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question