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

73
Visualizações
How do I change Component Attribute on onClick in React?

I want to change the lat and lng props on onClick but can't seem to find a way to target them, how do I do this?

const randomLocation = () => {
  // Here is where I want to target {lat} and {lng}
  Map.lat = 12.12312; //Example of what I mean
};

const App = () => {
  return (
    <div className="App">
      <header className="App-header">
        <Map lat={56.048095} lng={12.702583} />
        <div className="m-auto">
          <button
            onClick={randomLocation} // this is the onClick event
            className="inline-flex fixed bottom-16 left-[10rem] items-center justify-center px-5 py-3 border border-transparent text-base font-medium rounded-md text-white bg-[#2EC1EF] hover:bg-indigo-700 transition-all"
          >
            Teleport Me Somewhere Random
          </button>
          <button className="inline-flex fixed bottom-16 right-[10rem] items-center justify-center px-5 py-3 border border-transparent text-base font-medium rounded-md text-white bg-[#9A2EEF] hover:bg-indigo-700 transition-all">
            Take Me Home
          </button>
        </div>
      </header>
    </div>
  );
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think you'll want to create a variable for the lat and lng that's saved in state. Then onClick, you can set state.

const App = () => {
   state = { mapLat: 56.048095 }

  const randomLocation = () => {
    this.setState({ mapLat: 12.12312 })
  };


  return (
    <>
      <Map lat={mapLat} lng={12.702583} />
      <button
        onClick={randomLocation} // this is the onClick event
      >
        Teleport Me Somewhere Random
      </button>
    </>
  );
};

With useState hook:

const App = () => {
   const [mapLat, setMapLat] = React.useState(56.048095);

  const randomLocation = () => {
    setMapLat(12.12312)
  };


  return (
    <>
      <Map lat={mapLat} lng={12.702583} />
      <button
        onClick={randomLocation} // this is the onClick event
      >
        Teleport Me Somewhere Random
      </button>
    </>
  );
};
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