Typing useRef and Event Handlers
Handle DOM refs and synthetic events safely.
Typing useRef and Event Handlers is a free TypeScript Academy lesson on CoddyKit. This is lesson 3 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, and your progress syncs across the web and the CoddyKit app. The TypeScript Academy course includes 4 lessons in total.
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
Learn TypeScript with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 101
- Lessons
- 352
Frequently Asked Questions
Is the “Typing useRef and Event Handlers” lesson free?
Yes — the full text of “Typing useRef and Event Handlers” is free to read here on the web. 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. The TypeScript Academy course includes 4 lessons in total.
What will I learn in “Typing useRef and Event Handlers”?
Handle DOM refs and synthetic events safely. 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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “Typing useRef and Event Handlers” 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