-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
  • 1,717
  • 3
  • 18
  • 42
  • 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
  • 6,396
  • 1
  • 10
  • 19
1

you should change it to:

 setDisabled([...disabled,id])
Yasin Br
  • 1,313
  • 1
  • 3
  • 16
0

You could try something like this

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