小编ort*_*gon的帖子

无法使用 React router dom v6 beta 传递状态,状态为 null

应用程序.js 文件

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

索引.js 文件

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)

Posts.js 文件

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)

包.json …

javascript reactjs

25
推荐指数
3
解决办法
4万
查看次数

标签 统计

javascript ×1

reactjs ×1