I am trying to set a type
to a useState
default value.
...
export default () => {
const [dropdowns, setDropdowns] = useState({
websites: [{ label: String, value: Number }],
});
useEffect(() => {
setDropdowns({
websites: [
{
label: 'Google.com',
value: 1,
},
],
});
// TODO: Write clean function after api implementation is complete
return () => {};
}, []);
...
};
Why am I getting this error..?
3
Answers
Thanks to @Yewin, here is my final working code snippet.
Is setting the initial state to
{ websites: [{ label: String, value: Number }] }
instead of specifying the type. This causes TypeScript to incorrectly infer the type of the state as:useState
accepts a generic type to specify the type of the state:that showing errros bc of your data type is wrong
pls define correct types & update data like that you defined.
pls define type
example