0

I'm new to React. I'm trying to pass a value through URL using react-router-dom. But unable to get the params getting

TypeError: Cannot read property 'params' of undefined.

I tried the solutions that already present but nothing help. What is the mistake I did? Can someone explain it to me? Thanks in advance.

Main Component:

function App() {
  return (
    <div className="App">
      <Router>
        <Switch>
          <Route path="/edit/:id" component={Edit} exact={true}>
            <Edit />
          </Route>
        </Switch>
      </Router>
    </div>
  );
}
export default App;

Edit Component:

class Edit extends Component {
  constructor(props) {
    super(props);
    this.state = {
      id: this.props.match.params.id
    };
  }

  render() {
    return <h2>Edit Page</h2>;
  }
}
export default Edit;
3
  • console log this.props and check whether the match key exists in it or not? Commented Mar 2, 2020 at 12:35
  • and please mention the full link which you are using for edit. Commented Mar 2, 2020 at 12:41
  • I'm getting empty object. Commented Mar 2, 2020 at 16:59

3 Answers 3

3

Note: I provided my answer with useParams since your original question was involving it. Other than that if you have to use a class component, normally this.props.match.params.id should work since you are using your component via a Route.

You should use useParams in your Edit component and it should be a function component.

function Edit() {
  const { id } = useParams();
  return (
    <div>{id}</div>
  )
}

Also, I don't know which React Router version you use, but if you are not using v6, you probably want to use the Route part like this:

<Router>
  <Switch>
    <Route path="/edit/:id" component={Edit} exact />
  </Switch>
</Router>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, working fine with class component alone by removing component from in-between route tag.
By changing this to <Route path="/edit/:id" component={Edit} exact={true}><Edit /></Route> this <Route path="/edit/:id" component={Edit} exact={true}></Route>
0

Try this -

import { withRouter } from 'react-router-dom';

class Edit extends Component {
  constructor(props) {
    super(props);
    this.state = {
      id: this.props.match.params.id
    };
  }

  render() {
    return <h2>Edit Page</h2>;
  }
}
export default withRouter(Edit);

Comments

0

Need to check your react version because your code just ran fine in my system - Edited your Edit component

return (
            <>
            <h2>Edit Page</h2>
            {this.state.id}
            </>
        );

accessed url - http://localhost:3000/edit/:2

enter image description here

result in image

1 Comment

I'm using 16.13.0

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.