Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

How to setState with an Array and object in Typescript?

$
0
0

This is how I declare a state

const [input, setInput] = React.useState('')const [goals, setGoals] = React.useState<string[]>([])

and this is my error code:

const addHandler = () => {    setGoals((curGoals) => [...curGoals, { key: Math.random().toString(), value:input}])}

This is a typescript hint:

Argument of type '(curGoals: string[]) => (string | { key: string; value: string; })[]' is not assignable to parameter of type 'SetStateAction<string[]>'.Type '(curGoals: string[]) => (string | { key: string; value: string; })[]' is not assignable to type '(prevState: string[]) => string[]'.Type '(string | { key: string; value: string; })[]' is not assignable to type 'string[]'.Type 'string | { key: string; value: string; }' is not assignable to type 'string'.Type '{ key: string; value: string; }' is not assignable to type 'string'.ts(2345)

I still don't understand why my code still outputs this error. I did use a string type of array with useState.

How can I resolve this error?


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>