[英]this.props.history.push("/") isn't redirecting me to homepage
I am trying to redirect this login page to home page but for some reason this.props.history.push('/')
isn't redirecting it.我正在尝试将此登录页面重定向到主页,但由于某种原因this.props.history.push('/')
没有重定向它。 I have a handleSubmit
that is supposed to run after I press the login button.我有一个应该在我按下登录按钮后运行的handleSubmit
。 I am not really sure what is going on.我不确定发生了什么。 Pressing the login button runs the handleSubmit
but, it goes in the catch exactly at this.props.history.push('/')
.按下登录按钮会运行handleSubmit
,但它正好在this.props.history.push('/')
处。 Any help is appreciated.任何帮助表示赞赏。
App.js应用程序.js
class App extends Component {
render() {
return (
<MuiThemeProvider theme={theme}>
<Provider store={store}>
<Router>
{/* <Navbar /> */}
<div className="container">
<Routes>
<Route exact path="/" element={<Home/>} />
<Route exact path="/loginUser" element={<Login/>} />
<Route exact path="/createUser" element={<Signup/>} />
</Routes>
</div>
</Router>
</Provider>
</MuiThemeProvider>
);
}
}
export default App;
Login.js登录.js
class Login extends Component {
constructor(){
super();
this.state = {
email: '',
password: '',
errors: {}
}
}
handleSubmit = (event) => {
event.preventDefault();
const userData = {
email: this.state.email,
password: this.state.password
}
axios
.post("/loginUser", userData)
.then(res => {
console.log(res.data);
localStorage.setItem('FBIdToken', `Bearer ${res.data.token}`);
this.props.history.push('/')
})
.catch((err) => {
console.log("ERROR inside loginUser.js");
})
}
// Combine handleEmailChange and handlePasswordChange
handleEmailChange = (event) => {
this.setState({
email: event.target.value
})
}
handlePasswordChange = (event) => {
this.setState({
password: event.target.value
})
}
render() {
const { classes } = this.props;
return (
<Grid container className={classes.form}>
<Grid item sm/>
<Grid item sm>
<img src={HeroLogo} alt="CompanyLogo" className={classes.logo}/>
<Typography variant="h2" className={classes.pageTitle}>
Login
</Typography>
{/* Do we need to validate the email?? */}
<form noValidate onSubmit={this.handleSubmit}>
<FormControl margin="normal" variant="outlined" sx={{width:'50ch'}}>
<InputLabel htmlFor="email">Email</InputLabel>
<OutlinedInput
id="email"
type="email"
value={this.state.email}
className={classes.textField}
onChange={this.handleEmailChange}
label="Email"
/>
</FormControl>
<br/>
<FormControl margin="normal" variant="outlined" sx={{width:'25ch'}}>
<InputLabel htmlFor="password">Password</InputLabel>
<OutlinedInput
id="password"
type="password"
value={this.state.password}
className={classes.textField}
onChange={this.handlePasswordChange}
label="Password"
/>
</FormControl>
<br/>
<Button
type="submit"
variant="contained"
color="primary"
className={classes.button}
>
LOGIN
</Button>
<br/>
<small>Don't have an account? <Link to="/createUser">Sign up</Link></small>
</form>
</Grid>
<Grid item sm/>
</Grid>
)
}
}
package.json package.json
{
"name": "derms-frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"@material-ui/core": "^4.12.3",
"@material-ui/icons": "^4.11.2",
"@testing-library/jest-dom": "^5.16.1",
"@testing-library/react": "^11.2.7",
"@testing-library/user-event": "^12.8.3",
"axios": "^0.24.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-redux": "^7.2.6",
"react-router-dom": "^6.1.1",
"react-scripts": "4.0.3",
"redux": "^4.1.2",
"redux-thunk": "^2.4.1",
"web-vitals": "^1.1.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"proxy": "https://north*******************************cloudfunctions.net/api"
}
I'm surprised you're not seeing any errors for a couple reasons:我很惊讶您没有看到任何错误,原因如下:
react-router-dom
v6 no longer exposes a history
object to use for navigation. react-router-dom
v6 不再公开history
object 以用于导航。 It's replaced with a useNavigate
hook that returns a navigate
function.它被一个useNavigate
钩子取代,该钩子返回一个navigate
function。react-router-dom
v6 Route
components also no longer pass any route props ( ie history
, location
, and match
), they just don't exist. react-router-dom
v6 Route
组件也不再传递任何路由道具(即history
、 location
和match
),它们只是不存在。 In other words, this.props.history
is undefined and should throw an error when attempting to invoke the push
function.换句话说, this.props.history
是未定义的,在尝试调用push
function 时应该会抛出错误。 Since Login
is a class component you'll need to create your own custom withRouter
component to grab the navigate
function and pass it to Login
as a prop.由于Login
是一个 class 组件,您需要创建自己的自定义withRouter
组件来获取navigate
function 并将其作为道具传递给Login
。
const withRouter = Component => props => {
const navigate = useNavigate();
return (
<Component {...props} navigate={navigate} />
);
};
... ...
class Login extends Component {
constructor(){
super();
this.state = {
email: '',
password: '',
errors: {}
}
}
handleSubmit = (event) => {
event.preventDefault();
const userData = {
email: this.state.email,
password: this.state.password
}
axios
.post("/loginUser", userData)
.then(res => {
console.log(res.data);
localStorage.setItem('FBIdToken', `Bearer ${res.data.token}`);
this.props.navigate('/');
})
.catch((err) => {
console.log("ERROR inside loginUser.js");
})
}
...
render() {
...
return (
...
)
}
}
export default withRouter(Login);
import { Redirect } from 'react-router-dom';
const routes = [
{
path: '/',
exact: true,
component: () => <Redirect to='/homepage' />,
},
...allRoutes
];
this way you can redirect.这样你就可以重定向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.