Javontay Mcelroy
Javontay Mcelroy

Reputation: 25

React - How to redirect to another component on Search?

So I'm working on an app for school and I've been stuck on an issue for like two days. I'm building a search that gets data from TMDB, and all that works fine. When I type in the input all the data come flowing in! However, when I submit and try to redirect to the /results page which is linked to a component that displays the SearchResults when directed to, nothing happens and it stays on the homepage... I tried Redirect but either I'm using it incorrectly, or I shouldn't be using it in this case. Here's the code for my Search component, if you could help me out, I'd greatly appreciate it!:

import React, { Component, Redirect } from 'react';
import axios from 'axios';

class Search extends Component {
  state = {
    query: '',
    results: []
  };

  getInfo = () => {
    axios
      .get(
        `https://api.themoviedb.org/3/search/tv?api_key=6d9a91a4158b0a021d546ccd83d3f52e&language=en-US&query=${
          this.state.query
        }&page=1`
      )
      .then(({ data }) => {
        this.setState({
          results: data
        });
      });
  };

  handleInputChange = e => {
    e.preventDefault();
    this.setState(
      {
        query: this.search.value
      },
      () => {
        if (this.state.query && this.state.query.length > 1) {
          if (this.state.query.length % 2 === 0) {
            this.getInfo();
          }
        } else if (!this.state.query) {
        }
      }
    );
  };

  render() {
    return (
      <div>
        <form>
          <input
            className='search'
            placeholder='⌕'
            type='text'
            ref={input => (this.search = input)}
            onChange={this.handleInputChange}
          />
        </form>
        {this.state.results.length > 0 && (
          <Redirect
            to={{
              pathname: '/results',
              state: { results: this.state.results }
            }}
          />
        )}
      </div>
    );
  }
}

export default Search;

Upvotes: 1

Views: 4076

Answers (3)

Rahmat Anjirabi
Rahmat Anjirabi

Reputation: 968

"Redirect" component is inside the "react-router-dom" package. use this approach:

import { Redirect } from "react-router-dom";
import React, { Component } from 'react';

class Search extends Component {
//...

render() {

    const { results } = this.state
    const { props } = this.props

    return (results.length > 0 ? <Redirect
        to={{
            pathname: "/results",
            state: {
                results: results,
                from: props.location
            }
        }}
    /> : <form>
            <input
                className='search'
                placeholder='⌕'
                type='text'
                ref={input => (this.search = input)}
                onChange={this.handleInputChange}
            />
        </form>
    )
}

}

Upvotes: 0

victor.ja
victor.ja

Reputation: 888

You can use withRouter to get history prop injected and then do: history.push(“/results”)

The code will look something like this:

import React, { Component, Redirect } from 'react';
import axios from 'axios';
import { withRouter } from "react-router";

class Search extends Component {
  state = {
    query: '',
    results: []
  };

  componentDidUpdate(prevProps, prevState) {
  const { history } = this.props;
  if (prevState.results !== this.state.results) {
    history.push('/results');
  }
  }

  getInfo = () => {
    axios
      .get(
        `https://api.themoviedb.org/3/search/tv?api_key=6d9a91a4158b0a021d546ccd83d3f52e&language=en-US&query=${
          this.state.query
        }&page=1`
      )
      .then(({ data }) => {
        this.setState({
          results: data
        });
      });
  };

  handleInputChange = e => {
    e.preventDefault();
    this.setState(
      {
        query: this.search.value
      },
      () => {
        if (this.state.query && this.state.query.length > 1) {
          if (this.state.query.length % 2 === 0) {
            this.getInfo();
          }
        } else if (!this.state.query) {
        }
      }
    );
  };

  render() {
    return (
      <div>
        <form>
          <input
            className='search'
            placeholder='⌕'
            type='text'
            ref={input => (this.search = input)}
            onChange={this.handleInputChange}
          />
        </form>
      </div>
    );
  }
}

export default withRouter(Search);

This way you are programmatically navigating, using push method from react router's history.

Here you can read more about withRouter HOC: https://reacttraining.com/react-router/web/api/withRouter

Upvotes: 1

Russ Brown
Russ Brown

Reputation: 371

Are you using React Router for routing?. Do you need to change your URL? There isn't much of a need for something like this. Sending someone to /results is just old school unless you are going to do something like /results?q=pulp&amp;piction so someone can refresh or link directly to the results.

For something like this just display your result component

{this.state.results.length && (
  <SearchResults results={this.state.results} />
)}

If you are using a router and need to use the path for the school project school your teacher for dumb business requirements and give us more information about what you are using.

Upvotes: 1

Related Questions