Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data using state in Link tag in React router

I don't know where I'm wrong. I want to pass that parameter as a state to Link tag and get that in another component.

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

export default class CategoryList extends React.Component{


    render(){
        return(
            <tr>
                <td>
                    <Link to={{pathname:"/Topics/" + this.props.row.Occupation+"/"+this.props.row.objectId +'/id'+'/'+this.props.userName, state:{description:this.props.row.Description}}}>
                        {this.props.row.Occupation}
                    </Link>
                </td>
                <td>{this.props.row.Description}</td>
            </tr>
        )
    }
} 

index.js

<Route path="/Topics/:Occupation/:objectId/:id/:userName" component={Main} />

I want to pass it here and access the state given in Link tag. So your help will be great.

like image 327
Gowtham Koushik Avatar asked Sep 19 '18 11:09

Gowtham Koushik


2 Answers

After you pass data to the state object in the Link, you can access this data in the routed component(In your case it is the Main component), like this:

props.location.state
like image 149
Ron F Avatar answered Nov 15 '22 11:11

Ron F


You need not pass any extra values to the route. You can achieve what you desire by providing extra objects to the link object.

Something on the lines of -

 <Link to={{pathname:"/Topics/", customObject: customValue}}>

and then you can access it from this.props.location.customObject

like image 30
Aseem Upadhyay Avatar answered Nov 15 '22 11:11

Aseem Upadhyay