Back to Flow

Using React with Flow

website/docs/react/index.md

0.320.02.7 KB
Original Source

Developers will often use Flow and React together, so it is important that Flow can effectively type both common and advanced React patterns. This guide will teach you how to use Flow to create safer React applications.

In this guide we will assume you know the React basics and focus on adding types for patterns you are already familiar with. We will be using examples based on react-dom, but all of these patterns work in other environments like react-native as well.

Component and Hook Syntax {#toc-component-and-hook-syntax}

Flow has dedicated component and hook syntax for writing React components and hooks, and it is the standard way to write React with Flow. The syntax is enabled by default, gives components and hooks first-class type system support, and lets Flow enforce the Rules of React directly.

Make sure your toolchain is set up using the installation instructions, then read the Component Syntax and Hook Syntax docs. The older function and class component patterns still type-check, but are legacy: prefer component and hook syntax for new code.

Setup Flow with React {#toc-setup-flow-with-react}

Flow and Babel work well together, so it doesn't take much to adopt Flow as a React user who already uses Babel. If you need to setup Babel with Flow, you can follow this guide.

React Runtimes

Flow supports the @babel/plugin-transform-react-jsx runtime options required to use JSX without explicitly importing the React namespace.

If you are using the new automatic runtime, use this configuration in your .flowconfig so that Flow knows to auto-import jsx:

ini
[options]
react.runtime=automatic

See Also {#toc-see-also}