Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Router: pass props from one component to another using Navlink

I am using <NavLink> for routing now how can i pass props into <NavLink> to another Component

like image 311
Krishna Jangid Avatar asked Oct 26 '25 21:10

Krishna Jangid


1 Answers

The way you can pass data to navigated component using Link and NavLink

<Link to={{
   pathname:'/home',
   state: {name:'from home page'}  
 }}>
  Go to Home
</Link>

or using NavLink

<NavLink to={{
   pathname:'/home',
   state: {title:'from home page'}  
 }}>
  Go to Home
</NavLink>

inside navigated component you can access like this.

if it's hooks

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

    function App(){
      let location = useLocation();
      console.log(location)
      return <h2>...</h2>
    }

if it's class based component

console.log(this.props.location)
like image 187
akhtarvahid Avatar answered Oct 29 '25 18:10

akhtarvahid