-1

How do I append new value to the state array ?

I tried,

const [diabled, setDisabled] = useState([]);

const getValue = (id) => {
...
...
...
setDisabled(...disabled,id)
}

But doesn't seems to work.

Sai Krishnadas
  • 2,863
  • 9
  • 36
  • 69
  • 2
    Does this answer your question? [Correct modification of state arrays in React.js](https://stackoverflow.com/questions/26253351/correct-modification-of-state-arrays-in-react-js) – Sinan Yaman Sep 09 '21 at 11:25

3 Answers3

2

It should be spread inside an array:

Also, correct your spelling: disabled not diabled

setDisabled([...disabled,id])

And change your useState:

const [disabled, setDisabled] = useState([]);  // <-- Correct your spelling
Ryan Le
  • 7,708
  • 1
  • 13
  • 23
1

you should change it to:

 setDisabled([...disabled,id])
Yasin Br
  • 1,675
  • 1
  • 6
  • 16
0

You could try something like this

setDisabled((prevState) => [...prevState,id])
eth_lover
  • 61
  • 1