'Append value to array inside dictionary (useState) react js
I have a useState like this const [okLetters, setOkLetters] = useState({green: [], yellow: []})
I want to append values to each list under a condition
When I use setOkLetters({green: [...okLetters.green, grid[currAttempt.currRow][i]] and log okLetters to the console, no matter how many green letters there are, only one will be appended.
{green: Array(1), yellow: Array(0)}
green: ['A']
yellow: []
[[Prototype]]: Object
How can I append values to a list in this dictionary?
Solution 1:[1]
To make sure your updates are immutable, your syntax for updating state should be closer to
setOkLetters({...okLetters, green: [...okLetters.green, grid[currAttempt.currRow][i]]});
However, your current approach should be working as intended in appending letters to the green array. When/where are you logging okLetters? I'm wondering if there's an issue or if condition elsewhere that is causing only one green letter to be appended.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | gloo |
