- Hands-On Design Patterns with React Native
- Mateusz Grzesiukiewicz
- 428字
- 2021-08-13 15:12:59
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.
For instance, Example 2 is organized in the following way:
- src
- HelloBox.js
- HelloText.js
- makeExpandable.js
- App.js
- 數(shù)據(jù)科學實戰(zhàn)手冊(R+Python)
- 微信公眾平臺與小程序開發(fā):從零搭建整套系統(tǒng)
- JavaScript 網(wǎng)頁編程從入門到精通 (清華社"視頻大講堂"大系·網(wǎng)絡開發(fā)視頻大講堂)
- The Data Visualization Workshop
- 用Flutter極速構建原生應用
- 微信小程序項目開發(fā)實戰(zhàn)
- 利用Python進行數(shù)據(jù)分析
- 微信小程序全棧開發(fā)技術與實戰(zhàn)(微課版)
- 深度學習:Java語言實現(xiàn)
- 西門子S7-200 SMART PLC編程從入門到實踐
- Visual Foxpro 9.0數(shù)據(jù)庫程序設計教程
- Python大學實用教程
- Nagios Core Administration Cookbook(Second Edition)
- 大學計算機基礎實驗指導
- R的極客理想:量化投資篇