【发布时间】:2019-12-11 18:17:22
【问题描述】:
您好,在验证用户身份后,我无法找到可以通过推送调用路由的位置
我是用 redux 做的
我的行动:
const AuthenticateUser = (login, password) => {
return dispatch => {
dispatch (startAuth ());
// fetching data
api.post ('/ login', {login, password})
.then (response => {
localStorage.setItem ('token', response.data [0]);
dispatch (userAuthenticated (response.data [1]))
})
.catch (err => {console.error (err);});
}
}
export default AuthenticateUser;
我的减速机:
const authReducer = (state = initialState, action) => {
switch (action.type) {
case USER_AUTHENTICATED:
return {
... state,
loading: false,
authenticated: true,
user: action.user,
}
case USER_FAILED_AUTH:
return {
... state,
loading: false,
message: action.error
}
default:
return state;
}
}
还有我的表格
const SignIn = (props) => {
const classes = useStyles();
const dispatch = useDispatch();
const [login, setLogin] = useState('');
const [password, setPassword] = useState('');
const handleSubmit = (e) => {
e.preventDefault()
dispatch(auth(login, password))
}
return (
<div className={classes.root}>
<Grid container spacing={2} className={classes.gridMain}>
<Grid item lg={12} md={12} sm={12} xs={12} align="center">
<img src={require("../nodejs-icon.svg")} alt="bug" height={100} />
</Grid>
<Grid item lg={12} md={12} sm={12} xs={12} className={classes.TextField}>
<form onSubmit={handleSubmit}>
<TextField
className={classes.input2}
id="demo2"
label="Usuário"
variant="outlined"
value={login}
onChange={(e) => setLogin(e.target.value)}
InputLabelProps={{
classes: {
root: classes.label,
focused: classes.focusedLabel,
error: classes.erroredLabel
}
}}
InputProps={{
classes: {
root: classes.cssOutlinedInput,
focused: classes.cssFocused,
notchedOutline: classes.notchedOutline,
},
startAdornment: (
<InputAdornment position="start">
<PersonSharpIcon style={{ fontSize: 25, color: 'rgba(20, 176, 12,0.9)' }} />
</InputAdornment>
)
}}
/>
<TextField
className={classes.txtFd}
id="demo2"
label="Senha"
variant="outlined"
value={password}
onChange={(e) => setPassword(e.target.value)}
InputLabelProps={{
classes: {
root: classes.label,
focused: classes.focusedLabel,
error: classes.erroredLabel
}
}}
InputProps={{
classes: {
root: classes.cssOutlinedInput,
focused: classes.cssFocused,
notchedOutline: classes.notchedOutline,
},
startAdornment: (
<InputAdornment position="start">
<LockSharpIcon style={{ fontSize: 25, color: 'rgba(20, 176, 12,0.9)' }} />
</InputAdornment>
)
}}
/>
<ButtonBase variant="raised" disableFocusRipple="false" disableRipple="false" centerRipple="false">
<Typography noWrap className={classes.labelForgot} variant="subtitle2">
Esqueci minha senha
</Typography>
</ButtonBase>
<Button type="submit" className={classes.button} variant="raised" disableFocusRipple="false" disableRipple="false" centerRipple="false">
Entrar
</Button>
</form>
</Grid>
我有一个路由,在我验证此用户身份后,我想将其发送到路由或显示错误消息,但我不知道在代码中的何处执行此操作以及如何获取它。
我的路线
const AppRouter = () => (
<BrowserRouter>
<Route path="/xd" component={AuthPage} exact={true} />
<Route path="/dashboard/addProduct" component={AddProduct} exact={true} />
</BrowserRouter>
);
【问题讨论】:
-
为什么不将
push函数作为参数传递给 AuthenticateUser 函数(这个函数可以使用 camelCase,因为它不是组件 authenticateUser)?如果请求成功并在那里调用它?我假设您正在使用 react 路由器,因此如果您正在使用它,您可以使用 withRouter 包装器将其放入您的组件中,然后将其传入。 -
是的,我使用的是路由器浏览器,我用它编辑了答案,你能帮我怎么做吗?
-
在您的
SignIn组件中,您需要导入此import { withRouter } from "react-router";,然后您包装您的组件,例如像这样export default withRouter(SignIn);,您将在SignIn 组件的道具中拥有history来自路由器并包含push功能的道具,之后您可以在handleSubmit中使用该道具,类似于dispatch(auth(login, password, push)) -
并在我的函数上进行此登录: const SignIn = (props) => {
-
我做了这个并且我的函数登录有推送未定义我给 console.log 道具并得到了这个:/历史:{长度:2,动作:“POP”,位置:{...}, createHref: ƒ, push: ƒ, ...} location: {pathname: "/xd", search: "", hash: "", state: undefined} match: {path: "/xd", url: "/xd" , isExact: true, params: {…}} staticContext: undefined