Home > Net >  Correct way to push in string[] react typescript
Correct way to push in string[] react typescript

Time:05-05

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])
  • Related