[英]React useEffect hook has a missing dependency warning
Quick question, how do I fix the warning that I get when I try to use useEffect in my app.快速提问,我如何解决在我的应用程序中尝试使用 useEffect 时收到的警告。 Here is my code:
这是我的代码:
const userUrl = `${process.env.REACT_APP_API_URL}/${id}`;
const [newUser, setNewUser] = useState({
name: "",
email: "",
status: "",
gender: "",
});
useEffect(() => {
if (id) {
const getUser = async () => {
try {
const response = await axios.get(userUrl);
const apiData = response.data.data;
setNewUser({
...newUser,
name: apiData.name,
email: apiData.email,
status: apiData.status,
gender: apiData.gender,
});
} catch (err) {
console.error(err);
}
};
getUser();
} else {
setNewUser({
...newUser,
status: "active",
gender: "male",
});
}
}, [userUrl, id]);
This is the full warning message: React Hook useEffect has a missing dependency: 'newUser'. Either include it or remove the dependency array. You can also do a functional update 'setNewUser(n => ...)' if you only need 'newUser' in the 'setNewUser' call
这是完整的警告消息:
React Hook useEffect has a missing dependency: 'newUser'. Either include it or remove the dependency array. You can also do a functional update 'setNewUser(n => ...)' if you only need 'newUser' in the 'setNewUser' call
React Hook useEffect has a missing dependency: 'newUser'. Either include it or remove the dependency array. You can also do a functional update 'setNewUser(n => ...)' if you only need 'newUser' in the 'setNewUser' call
I tried adding the newUser inside the second parameter of the useEffect hook, but then I get the maximum depth error, so if anyone know how to fix this, I would gladly appreciate that.我尝试在 useEffect 钩子的第二个参数中添加 newUser ,但随后我得到了最大深度错误,所以如果有人知道如何解决这个问题,我将不胜感激。
The issue here is that you are referencing the newUser
state within the useEffect
callback, thus making it a dependency.这里的问题是您在
useEffect
回调中引用了newUser
状态,从而使其成为依赖项。 But you can't unconditionally update any value that is a dependency as this leads to the render looping you see when you added newUser
state as a dependency.但是您不能无条件地更新任何作为依赖项的值,因为这会导致您在添加
newUser
状态作为依赖项时看到的渲染循环。
You should use a functional state update on the state updater so you can remove the outer newUser
dependency.您应该在状态更新器上使用功能状态更新,以便您可以删除外部
newUser
依赖项。 The functional update enqueues a state update and passes the previous state value to the callback that can be used to compute the next state value.功能更新将状态更新排入队列,并将前一个状态值传递给可用于计算下一个状态值的回调。
useEffect(() => {
if (id) {
const getUser = async () => {
try {
const response = await axios.get(userUrl);
const { email, gender, name, status } = response.data.data;
setNewUser(newUser => ({ // <-- previous state
...newUser, // <-- shallow merge
name,
email,
status,
gender,
}));
} catch (err) {
console.error(err);
}
};
getUser();
} else {
setNewUser(newUser => ({ // <-- previous state
...newUser, // <-- shallow merge
status: "active",
gender: "male",
}));
}
}, [userUrl, id]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.