Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
React: How to redirect to another component before using history.push?

I am having a weird bug: After submitting a search form, my code redirects to the listing page and renders the products based on the search input.

This works on every site, except if i am already on the listing page. So i thought about redirecting first to the homepage, and after that redirecting to listing page, if i am already on /listing.

Unfortunatelly, using history.push if i am already on listing does not change anything. Only the second history.push() gets activated. Where could be my mistake?

(Even if i delete the if request, it does not redirect to "/". However, if i delete the second history.push, it does)

const SearchInput = ({history}) => {

  const [searchword, setSearchword] = useState('');



  const submitHandler = (e) => {
  e.preventDefault();

  console.log(history.location.pathname)

  if (history.location.pathname==="/listing"){
    history.push({
      pathname: HOME_PAGE,
      search: '?'+searchword, 
      state: { detail: 'some_value' } 
    });
  }

    history.push({
      pathname: LISTING_POSTS_PAGE,
      search: '?'+searchword, 
      state: { detail: 'some_value' }
      // 
    })
    
  
 }

  return (
    <form className="search" onSubmit={submitHandler}>
        <div className = "row">
          <input 
          type = "text"
          searchword = "q"
          id = "q"
          placeholder = "What do you want to buy?"
          onChange = {(e) => setSearchword(e.target.value)}>
          
          </input>
          
          
        </div>
      </form>
    
  );
};
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!