O'Reilly logo

React 16 Tooling by Adam Boduch

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Parents with primitive child values

It's common to render React components that take primitive values as children. In some cases, you might want to accept a string or a Boolean type. Here's how you would do this:

// @flow
import * as React from 'react';

type Props = {
  children?: React.ChildrenArray<string|boolean>,
};

const ParentWithStringOrNumberChild = ({ children }: Props) => (
  <section>
    <h2>Parent With String or Number Child</h2>
    {children}
  </section>
);
 
export default ParentWithStringOrNumberChild;

Once again, you can use the React.ChildrenArray type to specify that multiple child elements are allowed. To specify a specific child type, you pass it to React.ChildrenArray as a type argument—in this case a string and Boolean union. Now ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required