0

I am new to react and I try to pass Id from a component to another component. To do that I used

<a className="btn btn-view" href={`/buyer/viewpostdetails/${posts._id}`}>View Post <i className="fas fa-angle-double-right"></i></a>

this code. It works correctly and shows the URL correctly with the ID.

Then I tried to get that Id

    componentDidMount(){
            const id = this.props.match.params.id;

            axios.get(`/post/${id}`).then((res) => {
                if (res.data.success) {
                    this.setState({
                        post:res.data.post
                    });
                    console.log(this.state.post);
                }
            });
    }

I used the above code to do that but I got an error

TypeError: Cannot read property 'params' of undefined

How do I solve this issue?

4
  • Is that a simple link? What do you think this.props.match.params.id is? Are you using react-router? Commented Aug 1, 2021 at 19:30
  • @ZsoltMeszaros this is the URL that I got "localhost:3000/buyer/viewpostdetails/60f7d7fbd89c5a1470919a24". I want to get this id into the above id variable. That's why I used this "this.props.match.params.id". Commented Aug 1, 2021 at 19:36
  • Can you show the rest of the relevant code? Commented Aug 1, 2021 at 19:43
  • @yochanansheinberger this is the code that I used to do this constructor(props){ super(props); this.state={ post:{} }; } componentDidMount(){ const id = this.props.match.params.id; axios.get(/post/${id}).then((res) => { if (res.data.success) { this.setState({ post:res.data.post }); console.log(this.state.post); } }); } Commented Aug 1, 2021 at 19:53

1 Answer 1

1
import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";
import { useEffect, useState } from "react";
// import axios from "axios";

export default function App() {
  return (
    <div className="App">
      <Router>
        <Switch>
          <Route path="/" component={FirstPage} exact />
          <Route path="/:id" component={SecondPage} />
        </Switch>
      </Router>
    </div>
  );
}
function FirstPage() {
  const [post, setPost] = useState({ _id: 5 });
  return (
    <div>
      <div>First Page</div>
      <Link to={`/${post._id}`}>Link</Link>
    </div>
  );
}

function SecondPage(props) {
  const [state, setState] = useState();
  useEffect(() => {
    const id = props.match.params.id;
    setState(id);
    // axios.get(`/post/${id}`).then((res) => {
    //   if (res.data.success) {
    //     setState(res.data.post);
    //   }
    // });
  }, []);
  return <div>SecondPage id: {state}</div>;
}

You must do something like that.
First you define a router.
Then you can access the id from the relevant page or component.
And I suggest you to define components as functions and use useEffect and useState hooks.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much, @ibrahimAKIN. I appreciate your help!

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.