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

159
Visualizações
Clicking event increases value by two not one

I'm currently working on react state updates.

The data that I'm trying to update is a certain key-value pair in an array of objects.

When I click the thumbs-up button, the value in the 'likes' key must increase.

The problem I am facing is that the likes button will increase by 2 from the second click.

How should I handle this problem?

The code I was working on is:

import React, {useState} from 'react';
import './App.css';

function App() {

  const [data, setData] = useState([{id: 0, title: '남자 코트 추천', date: '2월 17일', likes: 0}, {id: 1, title: '여자 코트 추천', date:'2월 18일', likes:0}, {id: 2, title: '강남 맛집 추천', date:'2월 19일', likes:0},]);

  const handleLikesClick = (event) => {
    event.stopPropagation()
    setData((prevState)=>{
      const updateData = [...prevState]
      const id = parseInt(event.target.getAttribute("value"))
      updateData[id].likes += 1;
      return updateData
      })
    }

  return (
    <div className="App">
      <div className="black-nav">
        <div>개발 Blog</div>
      </div>
      {data.map(post => (
      <div className="list" key={post.id}>
        <h3>{post.title} <span onClick={handleLikesClick} value={post.id}>👍</span>{post.likes}</h3>
        <p>{post.date}</p>
        <hr/>
      </div>    
    ))}
  </div>    
  );
}

export default App;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem was that I had Strict Mode on in index.js, which makes my component to render twice.

I omitted Strict Mode in index.js and the problem was solved.

ReactDOM.render(<App />,
document.getElementById('root')
);
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