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

222
Visualizações
Reactjs change value when its clicked

I have a variable id that has already had value, I just want that if the user click the ClearIcon the id will turn to null. so that he does not become equal to location.id

  const [getId, setId] = useState(id)
  
  const stopSearching = () => {
    setId(null)
  };
  <IconButton onClick={stopSearching} edge="end">
    <ClearIcon />
  </IconButton>
  {console.log(getId)}
  {filteredLocations.map((location, index) => (
     <LocationWidgetItem
        key={index}
        location={location}
        onClickLocation={setActiveLocation}
          selectedLocation={location.id ==getId}
     />
  ))}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Is id a state variable?

Note: id === null is for equality comparison.

If is a state variable like

const [id, setId] = useState(initialValue);

then do it like this:

const stopSearching = () => {
    setId(null)
  };

If it is not a state variable, then to trigger rerender, it need to be a prop and there must be some function to change id prop.


Since I can't understand the query exactly, it will be better to add your code in codesandbox.io.

For now, I am adding a method,

const [getId, setId] = useState(0);
//I am taking id 0 as initial value, you can pick any other value
  
  const setActiveLocation = (id) => {
    setId(id);
  }
  const stopSearching = () => {
    setId(null)
  };
  <IconButton onClick={stopSearching} edge="end">
    <ClearIcon />
  </IconButton>
  {console.log(getId)}
  {filteredLocations.map((location, index) => (
     <LocationWidgetItem
        key={index}
        location={location}
        onClickLocation={() => setActiveLocation(location.id)}
          selectedLocation={location.id === getId}
     />)}
about 4 years ago · Juan Pablo Isaza Relatório

0

I think you have to set it with setId

const stopSearching = () => {
    setId(null)
};

so that will automatically invalidate the previous content and will set the

    selectedLocation={location.id ==getId}

to false

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