Issue
I have a component like this
type inputProps = {
value: string | number | boolean
onChange: (arg: string | number | boolean) => void
}
const Input = ({value, onChange}: inputProps) => {
return <div />
}
const OtherComponent = () => {
const [value, setValue] = React.useState(5)
// onChange type not compatible
return <Input value={value} onChange={setValue} />
}
const AnotherComponent = () => {
const [value, setValue] = React.useState('Hey')
// onChange type not compatible
return <Input value={value} onChange={setValue} />
}
Is it possible to make the arg
of the onChange
function dependent on the value?
Solution
Yes. It is possible using Generics:
type inputProps<T> = {
value: T;
onChange: (arg: T) => void;
};
const Input = <T,>({ value, onChange }: inputProps<T>) => {
return <div />;
};
Answered By - newProgrammer.cpp
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.