I'm using Types in React for the first time and I'm not very familiar with it.
I'm trying to add numbers from a form in a child component to an array of numbers.
Therefor I have created a useState hook:
const [valuesList, setValuesList] = useState<number[]>([]);
Now I'm trying to pass the setValuesList hook to the child component:
<AddNum
setValues={setValuesList}
/>
In the child component I'm defining an interface for the props:
interface AppProps {
setValues: (value: number) => void;
}
However when I try to call the setValues hook:
const addNumber = (value: string): undefined => {
const num = parseInt(value);
props.setValues((prevList) => prevList.concat(num));
return undefined;
};
I'm getting this error in the parent component:
/Users/acandael/Tutorials/react/add-nums/src/components/AddNum.tsx
TypeScript error in /Users/acandael/Tutorials/react/add-nums/src/components/AddNum.tsx(21,21):
Argument of type '(prevList: any) => any' is not assignable to parameter of type 'number[]'.
Type '(prevList: any) => any' is missing the following properties from type 'number[]': pop, push, concat, join, and 27 more. TS2345
19 | const addNumber = (value: string): undefined => {
20 | const num = parseInt(value);
> 21 | props.setValues((prevList) => prevList.concat(num));
| ^
22 | return undefined;
23 | };
24 |
Does anyone know how I can add numbers from a child component to an array of numbers in the parent component, and keep TypeScript happy?
Thanks for your help
num? Is that a number or an array of numbers?