Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not pass state with react router dom v6 beta, state is null

App.js file

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

expected to pass state,some piece of data from one page to another when using useLocation to get state from another page state is null

index.js file

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 file

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

Output

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

I can not either import switch from react-router-dom, it says switch is not exported from react router dom, I think have correctly set up my routes where is the error i can not pass state from one route to another

like image 937
ortagon Avatar asked Dec 22 '22 15:12

ortagon


2 Answers

I was able to get state by doing this:

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

I opened the type declaration and finally realized state was a prop, and not part of the to object https://github.com/ReactTraining/react-router/blob/dev/docs/api-reference.md#link

like image 161
chia berry Avatar answered Jan 11 '23 23:01

chia berry


I would like to add that in Version 5 you could get the state passed to the component by the props.

Now it only works using useLocation.

Component where the <Link /> is

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

Component where you want to get the state

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

const { state } = useLocation()

I found this article about that. https://ui.dev/react-router-pass-props-to-link/

I spend to many time around this and somehow i couldn't find any information about this on the documentation.

Hope to help someone out there

like image 30
Leandro Melo Avatar answered Jan 11 '23 23:01

Leandro Melo