0

When we use spread operator, then the new obj will be created. So what happens with old objects, if we press button many times?

 const [name, setName] = useState({ first: "Name", last: 0 });
  const [count, setCount] = useState(0);
  const increment = () => {
    setCount(count + 1);
    setName({ ...name, last: count });
  };
  return (
    <div>
      <p>
        Counter = {count}
        <button onClick={increment}>Set last name</button>
      </p>
      <p>
        {name.first} {name.last}
      </p>
    </div>
  );
}
truongt2
  • 7
  • 4

0 Answers0