1 回答
TA贡献1765条经验 获得超5个赞
UseEffect()像这样使用:
import { useHistory } from 'react-router-dom';
//
const LoginForm = ({
isAuthenticated,
redirectTo, // <-- here
error,
login,
clearErrors
}: ILoginForm) => {
const history = useHistory();
//
useEffect(() => {
if(redirectTo)
history.push(redirectTo);
});
}
const mapStateToProps = (state: IAuthReduxProps) => ({
isAuthenticated: state.auth.isAuthenticated,
redirectTo: state.auth.redirectTo, // <-- here
error: state.error
});
export default connect(mapStateToProps, { login, clearErrors })(LoginForm);
接口.ts:
export interface IAuthReduxProps {
auth: { isAuthenticated: boolean, redirectTo: string }; // <-- here added redirectTo
error: IError;
}
authReducer.ts:
case REGISTER_SUCCESS:
localStorage.setItem('token', action.payload.token);
return {
...state,
...action.payload,
isAuthenticated: true,
isLoading: false,
redirectTo: "/dd" // <-- here
};
添加回答
举报