0

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?

1 Answer 1

1

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])
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, but I still get the same error as described above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.