1

I need to update a property of an object inside an array with useState . But it don't update the quantity, where is the error?

( It is the initial state, i get all the rest of objects with axios from the server )

    const [data, setData] = useState([
      {
        codigo: 0,
        quantidadeAtual: 0,
        grptipo: "",
        grupo: 0,
        identificacao: "", 
        preco: ""
      }]);
function diminuirQuantidade(key: number) {
    setData(data.map(x => {
      let quantity = x.quantidadeAtual + 1;
      if (x.codigo !== key) return x
      return {...x, quantidadeAtual: quantity};
    }));
    }
{data.map((data) => {
            // console.log(data.codigo);
            if (data.identificacao === "") {
            } else {
              data.quantidadeAtual = 0;
              return (
                <div className={estilos.subcontainer} key={data.codigo}>
                  <div>
                    <div>

                      <div>
                        <h1>{data.identificacao}</h1>
                      </div>

                      <div className={estilos.containerBotao}>
                        <button onClick={() =>
                       diminuirQuantidade(data.codigo)}>-</button>
                        <h2>{data.quantidadeAtual}</h2>
                        <button>+</button>
                      </div>

                    </div>

                    <div>
                      <h2>{data.preco}</h2>
                    </div>
                    
                  </div>
                </div>
              );
            }
          })}

My data is like this:

[{ 
codigo: 308
grupo: 3
identificacao: "Água Mineral Com Gás"
preco: "R$ 3.50"
quantidadeAtual: 0
tipo: "Simples"
}, {...}]

I expected every time I click a button it decreases the quantity of one element. It don't show any error message.

6
  • 2
    It would help if you posted more of your code - where the hook is defined, what is triggering this update, etc Commented Apr 22, 2021 at 19:36
  • Are you sure the setData() call is being executed? Add a console.log() in that map function. Does it run? Commented Apr 22, 2021 at 19:37
  • Just wanted to highlight that you mention to decrease the quantity but you are actually incrementing the quantity - let quantity = x.quantidadeAtual + 1; Commented Apr 22, 2021 at 19:39
  • @swapnesh I make it only for test, but thanks for the comment Commented Apr 22, 2021 at 19:46
  • @Tore I edited now with more code. Commented Apr 22, 2021 at 19:47

1 Answer 1

2

The issue is that you set data.quantidadeAtual to 0 on render (in data.map((data) => ...)). This means that after pressing one of the buttons, you increment the quantity and then re-render (since state changed), which triggers the reset to 0. If you remove that line of code (and instead do that in an effect or wherever that logic belongs), it seems to work as expected.

Sign up to request clarification or add additional context in comments.

1 Comment

Very thanks, it solved the problem, i missed that line of code there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.