Generic Components and forwardRef
Build reusable typed components with generics.
Generic Components and forwardRef is a free TypeScript Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Welcome
Component Props Interface
interface ButtonProps {
label: string;
onClick: () => void;
disabled?: boolean;
}
function Button({ label, onClick, disabled }: ButtonProps) {
return <button onClick={onClick} disabled={disabled}>{label}</button>;
}FC vs Explicit Return Type
const Card: React.FC<{title: string}> = ({ title }) => <div>{title}</div>;
// Or explicit:
function Card2({ title }: { title: string }): JSX.Element {
return <div>{title}</div>;
}Typing useState
const [count, setCount] = useState(0); // number
const [name, setName] = useState(''); // string
const [user, setUser] = useState<User | null>(null); // explicitTyping useReducer
type Action = { type: 'increment' } | { type: 'decrement' } | { type: 'reset'; value: number };
function reducer(state: number, action: Action): number {
switch (action.type) {
case 'increment': return state + 1;
case 'decrement': return state - 1;
case 'reset': return action.value;
}
}Typing useRef for DOM
const inputRef = useRef<HTMLInputElement>(null);
// Use:
if (inputRef.current) {
inputRef.current.focus();
}Typing Event Handlers
function handleChange(e: React.ChangeEvent<HTMLInputElement>) {
console.log(e.target.value);
}
function handleClick(e: React.MouseEvent<HTMLButtonElement>) {
e.preventDefault();
}Generic Components
function List<T extends { id: number }>({ items }: { items: T[] }) {
return <ul>{items.map(i => <li key={i.id}>{JSON.stringify(i)}</li>)}</ul>;
}forwardRef with TypeScript
const Input = React.forwardRef<HTMLInputElement, InputProps>(
({ label, ...props }, ref) => (
<label>{label}<input {...props} ref={ref} /></label>
)
);Custom Hooks Return Types
function useToggle(init: boolean): [boolean, () => void] {
const [state, setState] = useState(init);
return [state, () => setState(s => !s)];
}Context with TypeScript
const AuthContext = React.createContext<User | undefined>(undefined);
function useAuth(): User {
const ctx = useContext(AuthContext);
if (!ctx) throw new Error('useAuth must be used within AuthProvider');
return ctx;
}Quick Check
Recap
Frequently asked questions
Is the “Generic Components and forwardRef” lesson free?
Yes — the full text of “Generic Components and forwardRef” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Generic Components and forwardRef”?
Build reusable typed components with generics. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Generic Components and forwardRef” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Typing React Component Props
- Typing useState and useReducer
- Typing useRef and Event Handlers
- Generic Components and forwardRef