0

My code

import React, { useState } from "react";
import "./styles.css";

export default function App() {
  const MAX_QUANTITY = 10;
  const MIN_QUANTITY = 0;
  const [count, setCount] = useState(0);
  const [items, setItems] = useState([{ id: 0 }]);

  const addItem = () => {
    setItems([
      ...items,
      {
        id: count + 1
      }
    ]);
    console.log(items);
  };

  const minusItem = () => {
    setItems([{ id: 0 }])

    console.log(items);
  }

  function add() {
    if (count < MAX_QUANTITY) {
      setCount(count + 1);
      addItem();
    }
  }

  function minus() {
    if (count > MIN_QUANTITY) {
      setCount(count - 1);
      minusItem()
    }
  }

  return (
    <div className="App">
      <h1>{count}</h1>
      <button onClick={add}>+</button>
      <button onClick={minus}>-</button>
    </div>
  );
}

My issue is that whilst I can consistently add an item to my items array, I am not able to remove the previous added item when the '-' button. As a test I set the 'minusItem' function to set the array back to it's initial state but you will notice that after the '-' button is pressed another object is added to the array before it is set, why is this?

I am just trying to add and subtract items to the array when either the '+' or '-' buttons are pressed. any suggestions on how to do this are most appreciated.

const {useState} = React;

function App() {
  const MAX_QUANTITY = 10;
  const MIN_QUANTITY = 0;
  const [count, setCount] = React.useState(0);
  const [items, setItems] = React.useState([{
    id: 0
  }]);

  const addItem = () => {
    setItems([
      ...items,
      {
        id: count + 1
      }
    ]);
    console.log(items);
  };

  const minusItem = () => {
    setItems(prevItems => prevItems.slice(0, -1));

    console.log(items);
  }

  function add() {
    if (count < MAX_QUANTITY) {
      setCount(count + 1);
      addItem();
    }
  }

  function minus() {
    if (count > MIN_QUANTITY) {
      setCount(count - 1);
      minusItem()
    }
  }

  return ( <
    div className = "App" >
    <
    h1 > {
      count
    } < /h1> <
    button onClick = {
      add
    } > + < /button> <
    button onClick = {
      minus
    } > - < /button> <
    /div>
  );
}

// Render it
ReactDOM.render(
  <App />,
  document.getElementById("react")
);
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>

<div id="react"></div>

counter example

2
  • Since you're not trying to remove specific elements in the array you could just pop() the last element in the array and update the state. Commented Jun 13, 2020 at 0:30
  • @ShawnYap could you provide a sudo code example to gain an idea to how this would look. Commented Jun 13, 2020 at 15:09

1 Answer 1

1

You want to remove the last item from the array, and assign that to the state. My preference would be to use slice

  const minusItem = () => setItems(prevItems => prevItems.slice(0, -1));
Sign up to request clarification or add additional context in comments.

2 Comments

I implemented your preference to the the live code snippet above, whilst it does work the console seem to indicate that an object gets added first before it is removed. e.g if you click '+' twice you will see that the array has two objects, click the '-' then an object is added to the array, but if i then press the '-' I then see two objects as expected. Is this the correct behavior?
Actually I think I may know whats going on. I think it might be the case that I am seeing in the console the current and previous states of the array and that is why I am seeing the effect. I removed the console log and placed them elsewhere in the function and now seeing the desired effect.

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.