[英]Can't perform a React state update on an unmounted component warning in Parent functional component when Child functional component redirects
I have a parent component UserLogInPage and a child component SignIn (both are functional components).我有一个父组件 UserLogInPage 和一个子组件 SignIn(都是功能组件)。
In SignIn child component, I redirect if authResult is true在 SignIn 子组件中,如果 authResult 为真,我会重定向
{authResult ? <Redirect to="/"></Redirect> : null}
and I trigger the loginUser action like so to check if the credentials a user enters is correct (this dispatch is in my handleSubmitSignInValues handler inside the SignIn child component):我像这样触发 loginUser 操作以检查用户输入的凭据是否正确(此调度位于 SignIn 子组件内的 handleSubmitSignInValues 处理程序中):
dispatch(loginUser(signInValues)).then((response) => {
console.log(response);
setAuthResult(response.payload.auth);
setMessage(response.payload.message);
setUserData(response.payload.userData);
});
My problem is that I keep getting the warning我的问题是我不断收到警告
Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
in SignIn (at UserLogInPage.component.jsx:59)
...
May I ask why this is happening and how I can fix this?请问为什么会这样,我该如何解决? This warning only started appearing after I tried redirecting.
此警告仅在我尝试重定向后才开始出现。 I've tried doing the redirect in a useEffect instead but that didn't help too.
我尝试在 useEffect 中进行重定向,但这也无济于事。
SignIn component登录组件
const SignIn = () => {
const classes = useStyles();
const dispatch = useDispatch();
const [signInValues, setSignInValues] = useState({
email: "",
password: "",
showPassword: false,
});
const [authResult, setAuthResult] = useState("");
const [message, setMessage] = useState("");
const [userData, setUserData] = useState({});
const handleChange = (prop) => (event) => {
setSignInValues({ ...signInValues, [prop]: event.target.value });
};
const handleClickShowPassword = () => {
setSignInValues({
...signInValues,
showPassword: !signInValues.showPassword,
});
};
const handleMouseDownPassword = (event) => {
event.preventDefault();
};
const handleSubmitSignInValues = (event) => {
if (signInValues.email === "" || signInValues.password === "") {
alert("Please enter all required fields");
return event.preventDefault();
} else if (signInValues.password.length < 8) {
alert("Your password is too short :( Make it longer than 8 maybe?");
return event.preventDefault();
}
console.log(signInValues);
dispatch(loginUser(signInValues)).then((response) => {
console.log(response);
setAuthResult(response.payload.auth);
setMessage(response.payload.message);
setUserData(response.payload.userData);
});
};
return (
<Container maxWidth={"sm"}>
<Typography component={"span"} className={classes.insideContainerStyle}>
<TextField
className={classes.textfieldStyle}
required
id="email"
label="Email"
onChange={handleChange("email")}
fullWidth
helperText="Enter your email here"
/>
<FormControl className={classes.textfieldStyle}>
<InputLabel htmlFor="standard-adornment-password">
Password * (min 8 characters)
</InputLabel>
<Input
required
id="standard-adornment-password"
type={signInValues.showPassword ? "text" : "password"}
value={signInValues.password}
onChange={handleChange("password")}
endAdornment={
<InputAdornment position="end">
<IconButton
aria-label="toggle password visibility"
onClick={handleClickShowPassword}
onMouseDown={handleMouseDownPassword}
>
{signInValues.showPassword ? (
<Visibility />
) : (
<VisibilityOff />
)}
</IconButton>
</InputAdornment>
}
/>
</FormControl>
<div></div>
<br></br>
<br></br>
<br></br>
<br></br>
<Button
onClick={(e) => handleSubmitSignInValues(e)}
variant="contained"
color="secondary"
>
Log Me In!
</Button>
<div></div>
...
{authResult ? <Redirect to="/"></Redirect> : null}
</Typography>
</Container>
);
};
export default SignIn;
Edit: Can I just ignore this?编辑:我可以忽略这个吗?
The response handler in the dispatch's then block is called asynchronously and when it's executing the component is no longer in the DOM (the redirect happened). dispatch 的 then 块中的响应处理程序被异步调用,当它执行时,组件不再在 DOM 中(发生了重定向)。 You're trying to update the state (
setAuthResult
, setMessage
etc) and React is warning you it can't perform a state update on an unmounted component.您正在尝试更新状态(
setAuthResult
、 setMessage
等)并且 React 警告您它无法对未安装的组件执行状态更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.