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

283
Visualizações
Getting and error called Invalid Hook call in react
import { useState } from 'react';
import "./Searchbar.css"
import SearchIcon from '@material-ui/icons/Search';
import MicIcon from '@material-ui/icons/Mic';
import { Button } from '@material-ui/core';
import { useHistory } from "react-router-dom";

function Searchbar() {
    const history = useHistory();
    const[input, setInput] = useState('');
   

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

        // when the click enter button 
        
        history.push("/search")
    }

    return (
        <form className="search">
            <div className="search_box">
                <SearchIcon className="search_icon" />
                <input value={input} onChange={e => setInput(e.target.value)}></input>
                <MicIcon />
            </div>

            <div className="search_cards">
               <Button type='submit' onClick={search} variant="outlined">Google Search</Button>
               <Button variant="outlined">I'm Feeling Lucky</Button>
            </div>
        </form>
    )
}

export default Searchbar;
"dependencies": {
    "@material-ui/core": "^4.12.3",
    "@material-ui/icons": "^4.11.2",
    "@testing-library/jest-dom": "^5.14.1",
    "@testing-library/react": "^11.2.7",
    "@testing-library/user-event": "^12.8.3",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-router-dom": "^5.3.0",
    "react-scripts": "4.0.3",
    "web-vitals": "^1.1.2"
  }, 

this is the my code. i have tried to use **useHistory** hook to redirect to the searchpage when i have clicked that search button.
but after adding Use History hook it displays message like

Invalid hook call. Hooks can only be called inside of the body of a function component.



is it wrong that the way i have called **useHistory** hook???


Github repo

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