What is the correct way to push elements in string[] in react?
I'm getting Type '(string | string[])[]' is not assignable to type 'string[]'
This is a playground link : Here
Am I not initializing the string correctly?
CodePudding user response:
You appear to be missing a square bracket in your example code:
| null>(['')
Should be
| null>([''])
Change line 8 so you're also spreading the second array, and it fixes your problem:
setArr((prev: string[])=> [...prev, ...tempArr])