7 October 2023

The spread operator (...) is a versatile tool in JavaScript that simplifies common operations like copying, merging, and adding elements to arrays and objects. It's widely used in modern JavaScript development and contributes to writing more concise and readable code. We can use it to append to array using a single liner. We use the previous state method, because that guarantees the correct result every time.

Source code viewer
  1. setState((prevState) => [...prevState, id]);
Programming Language: ECMAScript