0

I want to pass data from Component A to Component B from a Route Link and then make an API request in Component B and I was able to pass it from Component A to B but couldn't figure out how to pass that data from inside render to a method that will make an API Request. hopefully, I was clear please look at the code below. and thanx in advance.

Component A

<ul>
  {this.state.movies.map(movie => (
  <li key={movie.imdbID}>
   <img alt="img" src={movie.Poster} />
   <h1>{movie.Title}</h1>
   <p>{movie.Year}</p>
   <button>
   <Link to={{ pathname: "./productdetail", movieid: movie.imdbID }}>View More</Link></button>
  </li>))}
</ul>

Component B

class ProductDetailPage extends React.Component {
  state = {
    movieIdSearch: []
  };

  movieIdRequest(id) {
    axios.get(`http://www.omdbapi.com/?apikey=bcfe7e46&i=${id}`).then(res => {
      const movieById = res.data;
      this.setState({ movieIdSearch: movieById });
    });
  }

  render() {
    const {
      Poster,
      Title,
      Year,
      Released,
      Runtime,
      Genre,
      Country,
      Language,
      Actors,
      Plot
    } = this.state.movieIdSearch;
    return (
      <div>
        {/*how to pass   this.props.location.movieid to a movieIdRequest method*/}
        <img alt="img" src={Poster} />
        <h3>{Title}</h3>
        <div>
          <p>{Year}</p>
          <p>{Released}</p>
          <p>{Runtime}</p>
          <p>{Genre}</p>
          <p>{Country}</p>
          <p>{Language}</p>
        </div>
        <div>
          <h5>{Actors}</h5>
          <p>{Plot}</p>
        </div>
      </div>
    );
  }
}
Kinan Alamdar
  • 437
  • 1
  • 6
  • 22
  • Possible Duplicate of [How to pass params with history.push in react-router v4?](https://stackoverflow.com/questions/44121069/how-to-pass-params-with-history-push-in-react-router-v4/45263164#45263164) – Shubham Khatri Jan 30 '19 at 16:42
  • You may call movieIdRequest method in componentDidMount() – Hamed Jan 30 '19 at 17:35
  • Hamed I already solved it with this solution componentDidMount but anyway thank. – Kinan Alamdar Jan 30 '19 at 18:29

0 Answers0