useState 不在 axios promise 中执行
useSate doesn't execute in axios promise
这是 React 组件的一部分
const listId = props.listId;
const [list, setList] = useState({});
useEffect(() => {
let isMounted = true;
axios.get(`http://localhost:9000/lists/${listId}`).then((res) => {
if (isMounted) {
setList({ ...res.data });
}
});
return () => {
isMounted = false;
};
});
由于某种原因,setList 没有执行
看看这个codesandbox,它工作正常。
codesandbox Link: https://codesandbox.io/s/interesting-bush-5v3hf?file=/src/App.js
这是 React 组件的一部分
const listId = props.listId;
const [list, setList] = useState({});
useEffect(() => {
let isMounted = true;
axios.get(`http://localhost:9000/lists/${listId}`).then((res) => {
if (isMounted) {
setList({ ...res.data });
}
});
return () => {
isMounted = false;
};
});
由于某种原因,setList 没有执行
看看这个codesandbox,它工作正常。
codesandbox Link: https://codesandbox.io/s/interesting-bush-5v3hf?file=/src/App.js