I'm using React with Typescript and want to add uid to dataId array. this is my code.
type ElementData = {
uid: string;
description: string;
}
const Modal:React.FC<Props>= (props) => {
const [chipsState, setChipsState] = useState(false);
const [dataId, setDataId] = useState([]);
const onChipClick = (element:ElementData) => {
setChipsState(chipsState => !chipsState);
setDataId(dataId => [...dataId , element.uid]);
}
return (
<div>
</div>
);
}
export default Modal;
in this case, the following error show up with setDataId(dataId => [...dataId , element.uid]) here.
Argument of type '(dataId: never[]) => string[]' is not assignable to parameter of type 'SetStateAction<never[]>'.
Type '(dataId: never[]) => string[]' is not assignable to type '(prevState: never[]) => never[]'.
Type 'string[]' is not assignable to type 'never[]'.
Type 'string' is not assignable to type 'never'.
Please help me to fix this error. Thank you.
setDataId? Also are you aware that insetDataId(dataId => [...dataId , element.uid])you're taking an argument nameddataId- which overwrites the binding todataIdoutside. So thedataIdyou have inside this callback is not the same as the one outside. Is the callback tosetDataIdeven supposed to take an argument?[]has typenever[], you may want to type that more strictly usingassetDataId(preveId => [...preveId , element.uid]). Thank you.