How to update a state in react small project

Hello everyone
I’m stuck on the state update since.
I would like when I click on the update button to update the state by changing the values 1 and 2 to 5 and 10.

import React, {useState} from "react" ;
import "./styles.css";
export default function App() {

  const [state, setState] = useState({
    voiture: "renault",
    comment: [{score: 1, ip: "qwerty"}, {score: 2, ip: "shino"}]
  })
  const handleClick = () => {
    setState()
    
  }
  return (
    <div className="App">
      {state.comment.map((item, index) => {
       return( <div key={index}>{item.score}{item.ip}</div>)
      })}
      <button onClick={handleClick}>update</button>
    </div>
  );
}

I might suggest using multiple useState, or with more complex state objects using useReducer.

I guess you might do something like this.

const handleClick = () => {
  setState((state) => {
    return {
      ...state,
      comment: state.comment.map((user) => {
        return {
          ...user,
          score: user.score + 1,
        };
      }),
    };
  });
};
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.