11

I have a react component which is loaded on routing

I need to access a parameter from the url inside the components constructor

How do I do it?

can I access it like this:

class CustomCoponent extends React.Component {
    constructor(props,{match}) {
    }
}
1

5 Answers 5

10

You can access route parameter in react-router-dom v4.x, by getting params from the match props.

Where you define your routes,

import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';

...
<Router>
  <App>
    <Switch>
      <Route exact path="/" component={List} />
      <Route path="/:parameterToAccess" component={CustomComponent} />
    </Switch>
  </App>
</Router>
...

In your component,

class CustomComponent extends React.Component {
   constructor(props) {
     super(props);
     this.routeParam = props.match.params.parameterToAccess;
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Not working for react-router-dom 4.3.1. Getting Cannot read property 'params' of undefined error. Found a question here on stackoverflow but that's not much helpful
7

if you use routing then you can specify your route to expect the parameter.

 <Route path='/yourpath/:ParamName' component={CustomComponent}/> 

your component needs to be wrapped in the withRouter HOC for you to access this.

 import {withRouter} from 'react-router-dom';
 class CustomComponent extends React.Component{
   constructor(props){
   }
   //**ACCESS PARAMETER VALUE LIKE THIS**
   sample(){
      let Paramvalue=this.props.match.params.ParamName;
   }   
 }
 export default withRouter(CustomComponent);

2 Comments

what is the use of match in constructor(props,{match}) {}
when you wrap your commponent is withRouter, you get the match props, which contains all your url information like query strings etc
4

You can do it like this:

class CustomComponent extends React.Component {
    constructor({ match, ...props }) {
        console.log(match.params)
    }
}

Comments

0

As {match} is passed to the component as a prop(property) so we can access this prop as a normal prop way.

class CustomComponent extends React.Component {
console.log(this.props.match.url)
}

Comments

0

Routes

import logo from './logo.svg';
import './App.css';
import {BrowserRouter as Router,Route,Switch} from 'react-router-dom';

import UpdateEmployeeComponent from './components/UpdateEmployeeComponent';

function App() {
  return (
      <div>
        <Router>
          <div className="container">
            <Switch>
              <Route path="/update-employee/:id" component={UpdateEmployeeComponent}></Route>
            </Switch>
          </div>          
        </Router>
      </div>

  );
}

export default App;

Component

import React, { Component } from 'react';

class UpdateEmployeeComponent extends Component {
    constructor(props){
        super(props);
        this.state ={
            id : this.props.match.params.id                  
        }           
        console.log('Employee Id ::: '+this.id);
    }    

    render() {
        return (
            <div>
            
            </div>
        );
    }
}
export default UpdateEmployeeComponent;
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.