In typescript, I got error of
Type 'string[]' is not assignable to type 'never[]'.
I want to add 'abc' into the array, what's wrong with it?
export default function App() {
const [text, setText] = React.useState("abc");
const [arr, setArr] = React.useState([]);
const xx = () => setArr([...arr, text]);
return <div className="App">{text}</div>;
}
https://codesandbox.io/s/boring-spence-08g8r?file=/src/App.tsx:56-272
useState
is a generic function so use it to define the type of array.
like: const [arr, setArr] = React.useState<string[]>([]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With