0

I have array of objects (items) with button clickHandler function. When you click on button, it should add that object to new array named ‘myNewArray’. Please help me to achieve this. I added demo object inside array ‘myNewArray’. enter image description here

Explaination: If i click on category button '1-furniture', that object will added to new array named 'myNewArray'

import React, { useState } from "react";

const App = () => {
const [items, setItems] = useState([
{ name: "Furniture", categoryKey: 1 },
{ name: "Shoes", categoryKey: 2 },
{ name: "Electronics", categoryKey: 3 },
{ name: "Clothes", categoryKey: 4 },
{ name: "Grocery", categoryKey: 5 },
]);
const [myNewArray, setMyNewArray] = useState([{ name: "demo-item", categoryKey: 100 }]);

const clickHandler = (categoryKey: any) => {
console.log(categoryKey);
};

return (
<div>
  {items.map((item) => (
    <button onClick={() => clickHandler(item.categoryKey)} key={item.categoryKey}>
      {item.categoryKey}-{item.name}
    </button>
  ))}
  <h4>New array after clicking on item from above array[items]</h4>
  {myNewArray.map((item) => (
    <button onClick={() => clickHandler(item.categoryKey)} key={item.categoryKey}>
      {item.categoryKey}-{item.name}
    </button>
  ))}
</div>
);
};

export default App;

3 Answers 3

1

just use set method in the useState

const clickHandler = (item: any) => {
  setMyNewArray(prev => [...prev, {name:item.name,categoryKey: item.categoryKey}])
};

and pass item in the click

onClick={() => clickHandler(item)} 
Sign up to request clarification or add additional context in comments.

1 Comment

Instead of myNewArray, I should get name of the specific item clicked from object of array (items)
1

Here's the working solution:

import React, { useState, useEffect } from "react";

const App = () => {
  const [items, setItems] = useState([
    { name: "Furniture", categoryKey: 1 },
    { name: "Shoes", categoryKey: 2 },
    { name: "Electronics", categoryKey: 3 },
    { name: "Clothes", categoryKey: 4 },
    { name: "Grocery", categoryKey: 5 }
  ]);
  const [myNewArray, setMyNewArray] = useState([
    { name: "demo-item", categoryKey: 100 }
  ]);

  useEffect(() => {
    console.log(myNewArray);
  }, [myNewArray]);

  const clickHandler = (item) => {
    setMyNewArray([...myNewArray, item]);
  };

  return (
    <div>
      {items.map((item) => (
        <button onClick={() => clickHandler(item)} key={item.categoryKey}>
          {item.categoryKey}-{item.name}
        </button>
      ))}
      <h4>New array after clicking on item from above array[items]</h4>
      {myNewArray.map((item, i) => (
        <button key={i}>
          {item.categoryKey}-{item.name}
        </button>
      ))}
    </div>
  );
};

export default App;

The live demo is here: https://codesandbox.io/s/determined-solomon-phyy9u?file=/src/App.js:0-1163

You can have a look at the console to check the myNewArray updates. enter image description here

3 Comments

Instead of myNewArray, I should get name of the specific item clicked from object of array (items)
What do you means by Instead of myNewArray, I should get the name of the specific item clicked from object of array (items)? Then you need to return item.name;
Please have a look at the updated sandbox @TruptiGaonkar
0

You could also do like this.

if Item is with the matching category Key. Then, it's Update the myNewArray state with the new item

const clickHandler = (categoryKey: any) => {
    
const item = items.find((i) => i.categoryKey === categoryKey);
    
setMyNewArray([...myNewArray, item]);
  };

Here is Codesandbox

2 Comments

just passing item to clickHandler and setMyNewArray([...myNewArray, item]); should work.
Thanks. That's nice. Can we store these objects in new array myNewArray as refreshing page removes newly added buttons. I guess it will require backend to do that

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.