登录后,我想重定向到主页。 在表单中输入姓名和姓氏后,点击按钮后,如何跳转到首页?
import '../node_modules/bootstrap/dist/css/bootstrap.min.css';
import FormData from './components/form-data'
import { useState, useEffect } from "react";
import { BrowserRouter as Router, Route, Switch, useHistory} from 'react-router-dom';
//import { Redirect } from "react-router-dom";
import Home from './components/home'
//import Login from './components/login'
const USERDATA = localStorage.getItem('userdata')
? JSON.parse(localStorage.getItem('userdata'))
: []
function App() {
const [name, setName] = useState('')
const [surname, setSurname] = useState('')
const [userdata, setUserdata] = useState(USERDATA)
const history = useHistory();
const handleName = event => {
setName(event.target.value)
}
const handleSurname = event => {
setSurname(event.target.value)
}
const handleSubmitForm = event => {
event.preventDefault()
if (name !== '' && surname !== '') {
const data = { name, surname }
setUserdata(data)
setName('')
setSurname('')
history.push("/Home")
} else {
console.log('Hata')
}
}
useEffect(() => {
localStorage.setItem('userdata', JSON.stringify(userdata))
}, [userdata])
return (
<Router>
<main>
<Switch>
<Route path="/Home" >
<Home userdata={userdata} />
</Route>
<Route path="/" >
<FormData name={name}
surname={surname}
handleName={handleName}
handleSurname={handleSurname}
handleSubmitForm={handleSubmitForm}
/>
</Route>
</Switch>
</main>
</Router>
);
}
export default App;
/*<Route exact path="/Home" name={name}>
{FormData } ?
<Redirect to="/Form" /> :
<Home />}
</Route>
<Redirect to="/Home" />
<Redirect to={{
pathname: "/Form",
search: "utm=your+face"
}} />
<Redirect push to="/Home" />*/
登录后,我想重定向到主页。
答案 0 :(得分:0)
解决方案是让你为“/”路由做一个组件,而不是在 App 函数内做任何事情。
无论如何,您的应用应该只真正处理路由。以下工作:
import "./styles.css";
// import FormData from './components/form-data'
import { useState, useEffect } from "react";
import {
BrowserRouter as Router,
Route,
Switch,
useHistory,
useLocation
} from "react-router-dom";
//import { Redirect } from "react-router-dom";
// import Home from './components/home'
//import Login from './components/login'
const Root = () => {
const [name, setName] = useState("hello");
const [surname, setSurname] = useState("someone");
const [userdata, setUserdata] = useState(USERDATA);
const history = useHistory();
const handleSubmitForm = (event) => {
event.preventDefault();
if (name !== "" && surname !== "") {
const data = { name, surname };
setUserdata(data);
setName("");
setSurname("");
history.push({ pathname: "/Home", state: { ...data } });
} else {
console.log("Hata");
}
};
useEffect(() => {
localStorage.setItem("userdata", JSON.stringify(userdata));
}, [userdata]);
return <button onClick={handleSubmitForm}>Submit form</button>;
};
const Home = (props) => {
const location = useLocation();
return (
<div>
<p>I'm in home</p>
<p>name</p>
{location.state.name}
<p>surname</p>
{location.state.surname}
</div>
);
};
const USERDATA = localStorage.getItem("userdata")
? JSON.parse(localStorage.getItem("userdata"))
: [];
const App = () => {
return (
<Router>
<main>
<Switch>
<Route path="/Home">
<Home />
</Route>
<Route path="/">
<Root />
</Route>
</Switch>
</main>
</Router>
);
};
export default App;
如果你想把相关的状态传给首页那么你可以传入“pathname”和“state”给history.push
编辑:以防万一你想知道,我特意在 name 和 surname 状态中加入了“hello”和“someone”,这样我就可以快速按下一个按钮并触发 handleSubmitForm 函数。