1

I have an object array similar to below

[array1, setArray1] = useState(
        [
        {
          name: "somename",
          count: 0,
          division: "somedivision",
          cost: 1000,
        },
        {
          name: "secondname",
          count: 0,
          division: "somedivision",
          cost: 2000,
        },
      ]
)

How can i individually update the cost value of each object in my list?

I was thinking of something like setArray1((previous) => ({...previous, cost: newcost}))

But how can i do this for multiple objects in an object array?

6
  • 1. create a copy of sufficiently deep level 2. update it 3. call setArray1 Commented Jul 25, 2022 at 10:13
  • Does this answer your question? Updating multiple array object values with useState in React Commented Jul 25, 2022 at 10:13
  • @ChrisG The example link you provided shows a situation where they want to update the entire list value by 10 and i can understand the logic there. In my case, i need to update the data individually with different values based on values i get from another api call response. Commented Jul 25, 2022 at 10:19
  • @CodinRockz Then update the data individually in the callback. Commented Jul 25, 2022 at 10:21
  • Feel free to look for a better fit: list Commented Jul 25, 2022 at 10:21

1 Answer 1

0

const App = () =>{
  const [array, setArray] = React.useState([
        {
          name: "somename",
          count: 0,
          division: "somedivision",
          cost: 1000,
        },
        {
          name: "secondname",
          count: 0,
          division: "somedivision",
          cost: 2000,
        },
  ])
  const updateCosts = () => {
    let newCosts = [1002, 2006]
    setArray(previous => 
      previous.map((elem, index)=> {
        return({...elem, cost: newCosts[index]})
      })
     )
  }

  return(
    <div>
    <button onClick={()=>updateCosts()}>Update costs</button>
    { array.map(elem => <p key={elem.name}>{`Product ${elem.name} Cost ${elem.cost}`}</p>) }
    </div>
  )
}

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

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

8 Comments

can i use this without useEffects ?
@CodinRockz Why?
Why not ? @CodinRockz answer edited
cheers mate, can you also please add the previous code with useEffects, so i could compare. I will check and provide update soon. @JeanJacquesGourdin
The code is exactly the same, I mean just the display changed, the important part is in updateCost function .useEffect were used to apply a modification on render to avoid user interaction, I just put sequins on the code by asking user when to update. You can still have the previous version by checking the edits on the post
|

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.