FrontEnd web
Published in

FrontEnd web


How to pass state or data in react-router v6

Pass data one component another component with react-router (v6) Link Component.

How to pass state or data in react-router v6
How to pass state or data in react-router v6

The react-router is the best library to handle routing in reactjs. With react-router, you pass state or data from one component to another component to use the react-router Link component.

Data pass more quickly in the new version of react-router (v6). For example, you can pass an object's data into one component to another component.

You can play with code in code sandbox Browser base Ide.

Play with code


  1. Pass Data
  2. Access data

Pass Data

  1. First step
  2. Second step
  3. Third step

First Step

First step import Link Component in react-router-dom

import { Link } from "react-router-dom";

Second Step

In the second step, I create a state with the useState hook in reactjs and pass default data in useState.

const [data, setData] = useState({ id: "1t4",   title: " How to pass state in react-router-dom",   tag: ["reactjs", "react-router-dom"]});   // first exampleconst [data, setData] = useState(false); second example


You can pass anything in the react-router-dom Link component. I’m pass a object and boolean value for example purpose.

Third step

In the third step, we pass the state as an object from one component to another. in other words, we pass data present to the child.

// you pass state form of object
<Link to="/apple" state={{ data: data }} className="link">


Pass state as a object in react-router v6. if pass a state as a string, you a face a error.

Access data

Access data in the child component, the react-router (v6), provides a hook for accessing the data.

  1. Import hook
  2. Access data

Import hook

First, you import a useLocation hook, which component you access the data or state.

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

Access data

Use the useLocation react-router hook to access the state present to the child component.

import React from "react";
import { useLocation } from "react-router-dom";
export default function ApplePage(props) {const location = useLocation();console.log(props, " props");
console.log(location, " useLocation Hook");
const data = location.state?.data;return ( <div className="page"> 🍎 Page <h1> {data ? data.title : "Go to Home"} </h1> </div>);}
Acces state in react-router v6
Access data present to child component


Most of the time, new people pass state in link component and access to the props. So it is not working in react-router.


  1. You cant access the state directly to the child component. Instead, you see an undefined hook in the console.
  2. You cant access the react-router state in props.


I hope, My article solves your problem. If you have any queries, ask me in to comment section.

Feel free to like and share my article with others if you like my writing. You also tag on Twitter official_R_deep.

Read More content on the frontend web. Sign up for a free newsletter and join the frontend web community on medium.


For explanation, I use a code sandbox template and modify it according to my article.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store