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

214
Visualizações
counter does not increment or decrement react
import React, { useState } from 'react';


const Quantity = ({ todo }) => {
  const [count, setCount] = useState(+todo.count);
  function decrementCount() {
    setCount(count - 1)
    updateCount(count - 1)
  }

  function incrementCount() {
   setCount(count + 1)
   
   updateCount(count + 1)
 }

  // edit count function 

  const updateCount = async (newCount)  => {
    console.log("this is the newCount", newCount)  

  
  try {
     const body = { "newCount" : newCount}
     const response = await fetch(
      `http://localhost:5000/todos/${todo.todo_id}`,
       {
         method: "PUT",
         headers: { "Content-Type": "application/json" },
         body: JSON.stringify(body)
       }
     );
    
  

     window.location = "/";
    } catch (err) {
      console.error(err.message);
       
    }
   }


 return (
  <>
    <button onClick={() => decrementCount(todo.todo_id)}>-</button>
    <span>{count}</span>
    <button onClick= {() => incrementCount(todo.todo_id)}>+</button>
    </> 
  )
  }
  

export default Quantity;

I have created a counter app, however I am unable to get the incrementer and decrementer to work correctly, I have tried so many different things. Currently, whenever I change the count, the "items" in the description column gets deleted. I do not understand how this is happening. I have my current folder for this project on my github under fruitlist version 2 on my github

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

0

The issue was with your update function in index.js. Here is the updated code:

 app.put("/todos/:id", async (req, res) => {
     try {
         const { id } = req.params;
-        const { description } = req.body;
+        const { newCount } = req.body;
         const updateTodo = await pool.query(
-            "UPDATE todo SET description = $1 WHERE todo_id = $2",
-            [description, id]
+            "UPDATE todo SET count = $1 WHERE todo_id = $2",
+            [newCount, id]
         );
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