Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda