Function/Class Components
danger
Function and class component typing patterns are legacy. For new code, use Component Syntax instead.
Adding Flow types to your React components is incredibly powerful. After typing your component, Flow will statically ensure that you are using the component in the way it was designed to be used.
Functional Components
Adding Flow types to a functional component is the same as adding types to a standard function. Just create an object type for the props and Flow will ensure that the props passed to the component match up with what is expected.
1import React from 'react';2
3type Props = {4 foo: number,5 bar?: string,6};7
8function MyComponent(props: Props) {9 props.doesNotExist; // Error! You did not define a `doesNotExist` prop.prop-missingCannot get props.doesNotExist because property doesNotExist is missing in Props [1].10
11 return <div>{props.bar}</div>;12}13
14<MyComponent foo={42} />