# React TypeScript Cheatsheet
Cheatsheet for using React with TypeScript.
---
[**Web docs**](https://react-typescript-cheatsheet.netlify.app/docs/basic/setup) |
[Contribute!](https://github.com/typescript-cheatsheets/react/blob/main/CONTRIBUTING.md) |
[Ask!](https://github.com/typescript-cheatsheets/react/issues/new/choose)
:wave: This repo is maintained by [@eps1lon](https://twitter.com/sebsilbermann) and [@filiptammergard](https://twitter.com/tammergard). We're so happy you want to try out React with TypeScript! If you see anything wrong or missing, please [file an issue](https://github.com/typescript-cheatsheets/react/issues/new/choose)! :+1:
---
[![All Contributors](https://img.shields.io/github/contributors/typescript-cheatsheets/react-typescript-cheatsheet?color=orange&style=flat-square)](/CONTRIBUTORS.md) | [![Discord](https://img.shields.io/discord/508357248330760243.svg?label=&logo=discord&logoColor=ffffff&color=7389D8&labelColor=6A7EC2)](https://discord.gg/wTGS5z9)
- [The Basic Cheatsheet](https://react-typescript-cheatsheet.netlify.app/docs/basic/setup) is focused on helping React devs just start using TS in React **apps**
- Focus on opinionated best practices, copy+pastable examples.
- Explains some basic TS types usage and setup along the way.
- Answers the most Frequently Asked Questions.
- Does not cover generic type logic in detail. Instead we prefer to teach simple troubleshooting techniques for newbies.
- The goal is to get effective with TS without learning _too much_ TS.
- [The Advanced Cheatsheet](https://react-typescript-cheatsheet.netlify.app/docs/advanced) helps show and explain advanced usage of generic types for people writing reusable type utilities/functions/render prop/higher order components and TS+React **libraries**.
- It also has miscellaneous tips and tricks for pro users.
- Advice for contributing to DefinitelyTyped.
- The goal is to take _full advantage_ of TypeScript.
- [The Migrating Cheatsheet](https://react-typescript-cheatsheet.netlify.app/docs/migration) helps collate advice for incrementally migrating large codebases from JS or Flow, **from people who have done it**.
- We do not try to convince people to switch, only to help people who have already decided.
- ⚠️This is a new cheatsheet, all assistance is welcome.
- [The HOC Cheatsheet](https://react-typescript-cheatsheet.netlify.app/docs/hoc) specifically teaches people to write HOCs with examples.
- Familiarity with [Generics](https://www.typescriptlang.org/docs/handbook/2/generics.html) is necessary.
- ⚠️This is the newest cheatsheet, all assistance is welcome.
---
## Basic Cheatsheet
### Basic Cheatsheet Table of Contents
Expand Table of Contents
- [React TypeScript Cheatsheet](#react-typescript-cheatsheet)
- [Basic Cheatsheet](#basic-cheatsheet)
- [Basic Cheatsheet Table of Contents](#basic-cheatsheet-table-of-contents)
- [Section 1: Setup](#section-1-setup)
- [Prerequisites](#prerequisites)
- [React and TypeScript starter kits](#react-and-typescript-starter-kits)
- [Try React and TypeScript online](#try-react-and-typescript-online)
- [Section 2: Getting Started](#section-2-getting-started)
- [Function Components](#function-components)
- [Hooks](#hooks)
- [useState](#usestate)
- [useCallback](#usecallback)
- [useReducer](#usereducer)
- [useEffect / useLayoutEffect](#useeffect--uselayouteffect)
- [useRef](#useref)
- [Option 1: DOM element ref](#option-1-dom-element-ref)
- [Option 2: Mutable value ref](#option-2-mutable-value-ref)
- [See also](#see-also)
- [useImperativeHandle](#useimperativehandle)
- [See also:](#see-also-1)
- [Custom Hooks](#custom-hooks)
- [More Hooks + TypeScript reading:](#more-hooks--typescript-reading)
- [Example React Hooks + TypeScript Libraries:](#example-react-hooks--typescript-libraries)
- [Class Components](#class-components)
- [Typing getDerivedStateFromProps](#typing-getderivedstatefromprops)
- [You May Not Need `defaultProps`](#you-may-not-need-defaultprops)
- [Typing `defaultProps`](#typing-defaultprops)
- [Consuming Props of a Component with defaultProps](#consuming-props-of-a-component-with-defaultprops)
- [Problem Statement](#problem-statement)
- [Solution](#solution)
- [Misc Discussions and Knowledge](#misc-discussions-and-knowledge)
- [Typing Component Props](#typing-component-props)
- [Basic Prop Types Examples](#basic-prop-types-examples)
- [`object` as the non-primitive type](#object-as-the-non-primitive-type)
- [Empty interface, `{}` and `Object`](#empty-interface--and-object)
- [Useful React Prop Type Examples](#useful-react-prop-type-examples)
- [Types or Interfaces?](#types-or-interfaces)
- [TL;DR](#tldr)
- [More Advice](#more-advice)
- [Useful table for Types vs Interfaces](#useful-table-for-types-vs-interfaces)
- [getDerivedStateFromProps](#getderivedstatefromprops)
- [Forms and Events](#forms-and-events)
- [List of event types](#list-of-event-types)
- [Context](#context)
- [Basic example](#basic-example)
- [Without default context value](#without-default-context-value)
- [Type assertion as an alternative](#type-assertion-as-an-alternative)
- [forwardRef/createRef](#forwardrefcreateref)
- [Generic forwardRefs](#generic-forwardrefs)
- [Option 1 - Wrapper component](#option-1---wrapper-component)
- [Option 2 - Redeclare forwardRef](#option-2---redeclare-forwardref)
- [Option 3 - Call signature](#option-3---call-signature)
- [More Info](#more-info)
- [Portals](#portals)
- [Error Boundaries](#error-boundaries)
- [Option 1: Using react-error-boundary](#option-1-using-react-error-boundary)
- [Option 2: Writing your custom error boundary component](#option-2-writing-your-custom-error-boundary-component)
- [Concurrent React/React Suspense](#concurrent-reactreact-suspense)
- [Troubleshooting Handbook: Types](#troubleshooting-handbook-types)
- [Union Types and Type Guarding](#union-types-and-type-guarding)
- [Optional Types](#optional-types)
- [Enum Types](#enum-types)
- [Type Assertion](#type-assertion)
- [Simulating Nominal Types](#simulating-nominal-types)
- [Intersection Types](#intersection-types)
- [Union Types](#union-types)
- [Overloading Function Types](#overloading-function-types)
- [Using Inferred Types](#using-inferred-types)
- [Using Partial Types](#using-partial-types)
- [The Types I need weren't exported!](#the-types-i-need-werent-exported)
- [The Types I need don't exist!](#the-types-i-need-dont-exist)
- [Slapping `any` on everything](#slapping-any-on-everything)
- [Autogenerate types](#autogenerate-types)
- [Typing Exported Hooks](#typing-exported-hooks)
- [Typing Exported Components](#typing-exported-components)
- [Frequent Known Problems with TypeScript](#frequent-known-problems-with-typescript)
- [TypeScript doesn't narrow after an object element null check](#typescript-doesnt-narrow-after-an-object-element-null-check)
- [TypeScript doesn't let you restrict the type of children](#typescript-doesnt-let-you-restrict-the-type-of-children)
- [Troubleshooting Handbook: Operators](#troubleshooting-handbook-operators)
- [Troubleshooting Handbook: Utilities](#troubleshooting-handbook-utilities)
- [Troubleshooting Handbook: tsconfig.json](#troubleshooting-handbook-tsconfigjson)
- [Troubleshooting Handbook: Fixing bugs in official typings](#troubleshooting-handbook-fixing-bugs-in-official-typings)
- [Troubleshooting Handbook: Globals, Images and other non-TS files](#troubleshooting-handbook-globals-images-and-other-non-ts-files)
- [Editor Tooling and Integration](#editor-tooling-and-integration)
- [Linting](#linting)
- [Other React + TypeScript resources](#other-react--typescript-resources)
- [Recommended React + TypeScript talks](#recommended-react--typescript-talks)
- [Time to Really Learn TypeScript](#time-to-really-learn-typescript)
- [Example App](#example-app)
- [My question isn't answered here!](#my-question-isnt-answered-here)
- [Contributors](#contributors)
React.FC
not needed? What about React.FunctionComponent
/React.VoidFunctionComponent
?React.VoidFunctionComponent
or React.VFC
insteadReducer
from redux
HTMLElement
to usestate
twice?readonly
` already marks them as immutable. ([See PR and discussion!](https://github.com/DefinitelyTyped/DefinitelyTyped/pull/26813))
React.JSX.LibraryManagedAttributes
nuance for library authorsReact.FC
break defaultProps
?React.ReactNode
edge case before React 18