【发布时间】:2021-01-06 19:18:24
【问题描述】:
我编写了一个Login 组件,其中包含必须访问组件的history 对象的钩子useEffect:
export default (props: { children: React.ReactNode }) => {
const alertContext = useContext(AlertContext);
const authContext = useContext(AuthContext);
const { setAlert } = alertContext;
const { login, error, clearErrors, isAuthenticated } = authContext;
useEffect(() => {
if (isAuthenticated) {
props.history.push('/');
}
if (error === 'Invalid Credentials') {
setAlert(error, 'danger');
clearErrors();
}
// es-lint-disable-next-line
}, [error, isAuthenticated, props.history]);
即使我已将 React.ReactNode 定义为组件的 props 类型,我得到:
类型'{ children: ReactNode; 上不存在属性'history'; }'
此错误发生在这一行:
props.history.push('/');
我该如何解决这个问题?
【问题讨论】:
标签: reactjs typescript react-router