I tried to create an array in each click using ReactJS and Typescript. Here's my code:
const FormTags: React.FC = () => {
const [selected, setSelected] = React.useState<[]>([])
const handleTag = (e: React.FormEvent<HTMLInputElement>) => {
let id: number = +e.currentTarget.value
//working, show me each id
console.log(id)
// not working
setSelected([...id]) // error here
// Argument of type 'number' is not assignable to parameter of type
// 'SetStateAction<[]>'.
}
//retrieve tags working well, show me all list tags
return (
<form>
{tags && tags.map(tag: any) => (
<label key={tag.id}>
<input type="checkbox" value={tag.id} onClick={handleTag}/>
</label>
)}
</form>
)
}
I want to build an array like this [1, 2, 3, ...] in each click get ID of the tags and persist it with setSelected. How can I do it?
useState<number[]>([])