【问题标题】:Can not pass state with react router dom v6 beta, state is null无法通过反应路由器 dom v6 beta 传递状态,状态为空
【发布时间】:2021-07-07 16:39:17
【问题描述】:

App.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>
    )
}

期望传递状态,一些数据从一个页面到另一个页面 当使用 useLocation 从另一个页面获取状态时,状态为空

index.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')
);

Posts.js 文件

const location=useLocation()
console.log(location);

输出

Object { pathname: "/posts", search: "", hash: "", state: null, key: "hpuuzep5" }

package.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"
    ]
  }
}

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

【问题讨论】:

  • 这些只是您的路线,实际与预期的行为和相关代码是什么?
  • useLocations state prop 为 null 我必须将一些数据从一个页面传递到另一个页面
  • 那么你应该包含你试图传递数据和接收数据的代码。
  • 更新了我的问题
  • 当我尝试使用 react-router-dom v6 beta 传递状态时,我也得到了 null

标签: javascript reactjs


【解决方案1】:

我可以通过这样做获得状态:

<Link
  to={"/posts"}
  state={{test: 'test'}}>
    Posts
</Link>

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

【讨论】:

  • 应该是state={{ test: 'test' }}&gt;。需要将键值对包装在{}中。
  • @blackr1234 谢谢!我将编辑回复
【解决方案2】:

您还可以使用useNavigate() 挂钩导航到特定路径,如下所示:

const navigate = useNavigate();

navigate('/posts', {state: { test: 'test'}})

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

【讨论】:

    【解决方案3】:

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

    现在它只能使用 useLocation。

    &lt;Link /&gt; 所在的组件

    <Link to={'/page1'} state={{ state: 'mystate' }} >Page 1</Link>
    

    要获取状态的组件

    import { useLocation } from 'react-router-dom';
    
    const location = useLocation()
    
    const { state } = location.state
    

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

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

    希望能帮到别人

    【讨论】:

    • 就我而言: const state = location.state 有效。
    猜你喜欢
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-22
    • 2015-08-15
    相关资源
    最近更新 更多