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

232
Visualizações
ReactJS not updating state google map autocomplete fields

I have integrated google map auto-completed, it's working well, even i have console logged, every data is appearing as expected but the problem is, when i select, it doesn't update the state

Here you go for my component

import Autocomplete from "react-google-autocomplete";

const TestForm = () => {
    const [profile, setProfile] = useState({});

    const onPlaceSelected = (place) => {
      console.log(place)
      setProfile({...profile, "test": "test"})
      console.log(profile, )
    };

    const handleProfileChange = (e) => {
        setProfile({...profile, [e.target.name]: e.target.value})
      };

  
  return (
    <>
      
    <input onChange={handleProfileChange} type="text"></input>

    <Autocomplete
        apiKey="apikey"
        defaultValue={formData.profile.location || ""}
        onPlaceSelected={onPlaceSelected}
        />
                            
    </>
  );
};

You may notice, i used two different method to update state, one for general input fields and another for autocomplete lib, but general input field state is updateing but autocomplete state is not updating, i have console logged the place, i see it is appearing, only problem is when i select a location, the selected location doesnt add my state, what is the issue? why it's behaving so weirds? can anyone help me?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You cant log the state right after you updated it, because it's async. Try logging the state change in useEffect hook

const [profile, setProfile] = useState({});

const onPlaceSelected = (place) => {
  console.log(place)
  setProfile({...profile, "test": "test"})
};

useEffect(() => { console.log(profile) }, [profile])
about 4 years ago · Juan Pablo Isaza Relatório
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