September 2018
Intermediate to advanced
302 pages
7h 17m
English
React comes with support for basic type checking. It does not require you to upgrade to TypeScript or another, more advanced solution. To achieve type checking straight away, you can use the prop-types library.
Let's provide type definitions for our HelloBox component from Chapter 1/Example 12:
import PropTypes from 'prop-types';// ...HelloBox.propTypes = { isExpanded: PropTypes.bool.isRequired, expandOrCollapse: PropTypes.func.isRequired, containerStyles: PropTypes.object, expandedTextStyles: PropTypes.object};
This way, we force isExpanded to be of the Boolean type (true or false), and expandOrCollapse to be a function. We also let React know about two optional style props (containerStyles and expandedTextStyles ...
Read now
Unlock full access