export default function App() {
return (
<div className="h-100">
<Routes>
<Link to={
{
pathname: "/posts",
state: {test: 'test'}
}
}>Posts</Link>
<Route path="/" element={<Home/>}/>
<Route path="/login" element={<Login/>}/>
<Route path="/posts" element={<Posts/>}/>
</Routes>
</div>
)
}
Run Code Online (Sandbox Code Playgroud)
期望传递状态,当使用 useLocation 从另一个页面状态获取状态时,从一个页面到另一个页面的某些数据为 null
import {BrowserRouter as Router} from "react-router-dom";
import App from "./App";
ReactDOM.render(
<React.StrictMode>
<Router>
<App/>
</Router>
</React.StrictMode>
,
document.getElementById('root')
);
Run Code Online (Sandbox Code Playgroud)
const location=useLocation()
console.log(location);
Run Code Online (Sandbox Code Playgroud)
Object { pathname: "/posts", search: "", hash: "", state: null, key: "hpuuzep5" }
Run Code Online (Sandbox Code Playgroud)