I'm trying to make a React component which takes in a generic type parameter which will be part of its prop type.
I want a solution that would look something like this:
interface TestProps<T> {
value: T;
onChange: (newValue: T) => void;
}
const Test: React.FC<TestProps<T>> = (props) => (
<span>{props.value}</span>
);
I have seen that there is support for this in TypeScript 2.9 and I'm on 4.3.5.
Usage of this would look like this:
const Usage: React.FC = () => (
<div>
<Test<Obj>
value={{ name: 'test' }}
onChange={(newValue) => {
console.log(newValue.name);
}}
/>
</div>
);
Code sandbox: https://codesandbox.io/s/react-typescript-playground-forked-8hu13?file=/src/index.tsx
React.FC
helper type:function Test<T>(props: TestProps<T>) { ...
codesandbox.io/embed/… – Chime