1

I'm not an expert in React Js and want to know the difference between these two syntaxes in the ES6 syntax.

const items = array.map(thing => <p key={thing}>{thing}</p>)
function addItem(){
    setArray(previtems  => { return [...previtems, `Thing ${array.length + 1}`]});
  }

and

const items = array.map(thing => <p key={thing}>{thing}</p>)
function addItem(){
    setArray([...items, `Thing ${array.length + 1}`]);
  }

1 Answers1

2

The first one is functional setState. It gives guarantee that the value of prevItems will be the most updated one. This pattern is to be used whenever next state depends on the previous state.

Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39