0

I'm trying to create a array which contains a bunch of objects like this and insert them upon different actions:

[
{price: 'price_1M73OzIRkO5W1JretwretkAHa', quantity: 1},
{price: 'price_1M73OzI4545W1JretwretkAHa', quantity: 3},
{price: 'price_1M73OzIRkO4545twr45kAHa', quantity:2}
]

However I cant seem to add in a third object it just overwrittes the second one.

const [allPlans, setAllPlans] = useState([]);

setAllPlans([{...allPlans, price:'price_1M73OzIRkO5W1JretwretkAHa', quantity: 1}]);

Thank you for the help!

update: Early in the code I do do this to update the quanity on the orginal entry. Could this be creating the issue?

setAllPlans({...allPlans,"quantity": userAmount});

Update: Here is a link to a working example of the problem corrected: https://codesandbox.io/s/priceless-ully-594ihs?file=/src/App.js:280-287

2
  • You want to spread out the array of existing objects, and add a new object to that array: setAllPlans([ ...allPlans, newObj ]); Commented Nov 23, 2022 at 19:34
  • Is allPlans initially set to that array you mentioned? Commented Nov 23, 2022 at 20:14

2 Answers 2

2

Try this method:

setAllPlans(allplans => [...allplans, {price:'price_1M73OzIRkO5W1JretwretkAHa', quantity: 1}]);

Here's an additional link demonstrating how this works: https://codesandbox.io/s/react-hooks-playground-forked-ooi7og?file=/src/index.tsx

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

14 Comments

You really need to test the code before you post it. This won't work.
edited, why wont it work?
Well, it will work now. But your previous edit would not work.
Please add an explanation of why this works but the OPs code doesn't. Teach a man to fish and whatnot.
That actually gives me this error: allplans is not iterable
|
0

Maybe there is a cleaner way to do it, but you can try to do it with push method where prevPlans is going to be your initial array and you can add specific objects into that array.

setAllPlans((prevPlans) => [
      prevPlans.push({
        price: "i'm new here",
        quantity: 1
      })
    ]);

const {
  useState
} = React;

const App = () => {
  const [allPlans, setAllPlans] = useState([{
      price: "price_1M73OzIRkO5W1JretwretkAHa",
      quantity: 1
    },
    {
      price: "price_1M73OzI4545W1JretwretkAHa",
      quantity: 3
    },
    {
      price: "price_1M73OzIRkO4545twr45kAHa",
      quantity: 2
    }
  ]);

  const addtoArray = () => {
    //prevPlans is your actual array and you push new object inside it and set your state
    setAllPlans((prevPlans) => [
      prevPlans.push({
        price: "i'm new here",
        quantity: 1
      })
    ]);
    console.log(allPlans);
  };

  return ( <
    div >
    <
    button onClick = {
      () => addtoArray()
    } > Click me < /button> <
    /div>
  );

}


// Render it
ReactDOM.createRoot(
  document.getElementById("root")
).render( <
  App / >
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.1.0/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.1.0/umd/react-dom.development.js"></script>

Comments

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.