1

I'm using tmdb api multi search, I have a get request that returns back an array of items which have an object key and value. Some return 'media_type: tv', some return 'media_type: movie' I have two different routes set up for each of these.

Heres my App structure

<BrowserRouter>
    <div className='App'>
      <Switch>
        <Route path='/' exact component={Home} />
        {/* <Route exact path='/details/:type/:id' component={ItemDetails} /> */}
        <Route exact path='/details/movie/:id' component={MovieDetails} />
        <Route exact path='/details/tv/:id' component={TvDetails} />
      </Switch>
    </div>
  </BrowserRouter>

Get Request

` performSearch = () => { // Requesting data from API
    let now = (this.now = Date.now());
    axios.get(`${URL}api_key=${API_KEY}&language=en-US&query=${this.state.searchInput}${PARAMS}`)
        .then((res) => {
            console.log(res.data.results);
            // Accepting response if this request was the last request made
            if (now === this.now) {
               this.setState({ searchResults: res.data.results});
            }
        });
}`

Functional Component that renders search result items

    const Suggestions = (props) => {
  const options = props.searchResults.map(r => (
    <li
      key={r.id} >
      <Link key={r.id} to={`/details/${r.id}`}>
        <a href='#t' className='rating'><i className='fas fa-star fa-fw' />{r.vote_average}</a>
        <img src={`https://image.tmdb.org/t/p/w185/${r.poster_path}`} alt={r.title} className='search-results-poster' />
        <a href='#t' className='search-results-name'>{r.name}</a>
        <a href='#t' className='search-results-title'>{r.title}</a>
      </Link>
    </li>
  ))
  return <ul className='search-results'>{options}</ul>
}

I'm thinking something like if(props.media_type === tv){<Redirect component={TvDetails}/> else {<Redirect component={MovieDetails}, I'm new to react-router so I'm not sure on how to do this

I have tried <Link key={r.id} to={/details/${props.searchResults.media_type.value}/${r.id}}>

but it says value is undefined though I can see it in my console.log enter image description here

edit: <Link key={r.id} to={/details/${r.media_type.value}/${r.id}}>

Takes me to /details/undefined/1234 , So I guess the value is still undefined?

1
  • I just got it working, though I used <Link key={r.id} to={/details/${r.media_type}/${r.id}}> Thanks though Commented Dec 19, 2018 at 4:21

2 Answers 2

1

media_type itself returns the string value in your case and an object with attribute value. So you just need to access media_type and not media_type.value

<Link key={r.id} to={`/details/${r.media_type}/${r.id}}`>
Sign up to request clarification or add additional context in comments.

Comments

1

Change
<Link key={r.id} to={/details/${r.media_type.value}/${r.id}}>
to
<Link key={r.id} to={/details/${r.media_type}/${r.id}}>.
You don't need to add .value to get the value from the object

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.