官术网_书友最值得收藏!

Type checking with PropTypes

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). If styles are not provided, we simply return the default styles.

There is also a neat feature to avoid explicit if in the markup—default props. Check it out:

HelloBox.defaultProps = {
containerStyles: styles.container,
expandedTextStyles: styles.text
};

Cool! Now, if containerStyles or expandedTextStyles are be null, then they will get a respective default value. However, if you run your application now, you will notice a little warning:

Warning: Failed prop type: Invalid prop `containerStyles` of type `number` supplied to `HelloBox`, expected `object`.

You may be freaking out right now, but this is correct. This is a nice optimization that has been made by the React Native team that you may not be aware of. It caches the stylesheet and simply sends the cached ID. The following line is returning the number and ID of a stylesheet that represents the styles object that was passed:

styles.container

Hence, we need to adapt our type definitions:

HelloBox.propTypes = {
isExpanded: PropTypes.bool.isRequired,
expandOrCollapse: PropTypes.func.isRequired,
containerStyles: PropTypes.oneOfType([
PropTypes.object,
PropTypes.number

]),
expandedTextStyles: PropTypes.oneOfType([
PropTypes.object,
PropTypes.number

])
};

Now, you can remove explicit if statements in the component markup. It should look more or less like this:

export const HelloBox = ({
isExpanded,
expandOrCollapse,
containerStyles,
expandedTextStyles
}) => (
<View style={containerStyles}>
<HelloText onPress={() => expandOrCollapse()}>...</HelloText>
<HelloText onPress={() => expandOrCollapse()}>...</HelloText>
{
isExpanded &&
<HelloText style={expandedTextStyles}>
...

</HelloText>
}
</View>
);

Good job! We have defined default props and type checks for our component. Please check the full working Example 2 in the src/chapter 2 directory for more details.

Please note that, from now on, all code examples will be split into a few modular source files. All files will be placed under the  ./src directory of the respective example.
For instance, Example 2 is organized in the following way:
  • src
    • HelloBox.js
    • HelloText.js
    • makeExpandable.js
  •  App.js
This structure will evolve as the application grows. In Chapter 10, Managing Dependencies, you will learn how to organize files in big projects with over one million lines of code.
主站蜘蛛池模板: 榆社县| 清远市| 治县。| 绥棱县| 广宗县| 喜德县| 肇东市| 迭部县| 阳城县| 绥宁县| 阿勒泰市| 平江县| 伊宁市| 方正县| 中方县| 克什克腾旗| 宁津县| 嘉义市| 白城市| 德化县| 新田县| 思南县| 教育| 宣恩县| 高安市| 凭祥市| 新丰县| 龙海市| 高碑店市| 葵青区| 普宁市| 三门峡市| 永州市| 元氏县| 乐至县| 芦溪县| 三都| 和田县| 靖宇县| 长子县| 田林县|