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

ort*_*gon 25 javascript reactjs

应用程序.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

{
  "name": "chance",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.9",
    "@testing-library/react": "^11.2.5",
    "@testing-library/user-event": "^12.6.3",
    "axios": "^0.21.1",
    "bootstrap": "^5.0.0-beta3",
    "history": "^5.0.0",
    "ramda": "^0.27.1",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-router": "^5.2.0",
    "react-router-dom": "^5.2.0",
    "react-scripts": "4.0.2",
    "styled-components": "^5.2.3",
    "web-vitals": "^1.1.0"
  },
  "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"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

我无法从react-router-dom导入交换机,它说交换机没有从react router dom导出,我认为已经正确设置了我的路由,错误在哪里,我无法将状态从一个路由传递到另一个路由

chi*_*rry 43

我能够通过这样做来获得状态:

<Link
  to={"/posts"}
  state={{test: 'test'}}>
    Posts
</Link>
Run Code Online (Sandbox Code Playgroud)

我打开类型声明,最终意识到状态是一个道具,而不是对象的一部分https://github.com/ReactTraining/react-router/blob/dev/docs/api-reference.md#link

  • 应该是 `state={{ test: 'test' }}&gt;`。需要将键值对包裹在“{}”中。 (3认同)

小智 16

我想补充一点,在版本 5 中,您可以通过 props 将状态传递给组件。

现在它只能使用 useLocation 来工作。

的组件<Link />

<Link to={'/page1'} state={{ state: 'mystate' }} >Page 1</Link>
Run Code Online (Sandbox Code Playgroud)

您想要获取状态的组件

import { useLocation } from 'react-router-dom';

const { state } = useLocation()
Run Code Online (Sandbox Code Playgroud)

我找到了这篇文章。 https://ui.dev/react-router-pass-props-to-link/

我花了很多时间解决这个问题,但不知何故我在文档中找不到任何有关此的信息。

希望能帮助那里的人


Djo*_*jov 11

您还可以使用钩子导航到某个路径,useNavigate()如下所示:

const navigate = useNavigate();

navigate('/posts', {state: { test: 'test'}})
Run Code Online (Sandbox Code Playgroud)

完整的解释在这里记录得很好: https://reactrouter.com/docs/en/v6/upgrading/v5#use-usenavigate-instead-of-usehistory