How to pass parameters via url to an application in React?

Asked

Viewed 7,589 times

2

I have an application in .Net MVC where I use the Session to store logged user data.

But within this application I have a screen made in React which will be accessed externally (that is, it is a separate project), but I need to pass the ID of the logged in .NET to the React.

How to carry this data and recover in React by url ?

Example of . Net URL:

 <li class="filtro"><a href="https://localhost:44324/@Session["IdUsuarioLogado"]" target="_blank"><i class="fas fa-tachometer-alt "></i> Operação</a></li>

https://localhost/74

Main screen that will receive the Operation.js parameter (React):

import React, { Component } from 'react';


export default class Operacao extends Component {

    constructor(props) {

       super(props);


    }

    componentWillMount() {

    }


    componentDidMount() {



    }

    render() {
        return (
            <div> 
            </div>
        );

    }

}

2 answers

6


Whereas you’re using the React Router:

In the route configuration, put the name of the parameter at the end of the path along with two points(:). Ex.: path="/:id"

<Route exact path="/:id" component={ component} />

In the component you take the parameter through the props: this.props.match.params.id

Obs. id is an optional name, you can put the name you want.

For more details, see official documentation.

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.