- Hands-On Design Patterns with React Native
- Mateusz Grzesiukiewicz
- 319字
- 2021-08-13 15:12:58
Examples of useful HOCs
Do you need a quick logger that will show you how your app behaves? Or maybe you are preparing a live presentation and you want to show some dynamic information on a screen? Here we go:
// src/ Chapter_1/ Example_16_Useful_HOCs/ App.js
const logPropChanges = Component => props => {
console.log('[Actual props]:', props);
return <Component {...props} />;
};
// Use: makeExpandable(logPropChanges(mapPropNames(SomeSection)));
Okay, good. Now, let's suppose that you are waiting on some data to load. Here comes the spinner:
// src/ Chapter_1/ Example_16_Useful_HOCs/ App.js
import {ActivityIndicator} from 'react-native';
const withSpinner = Component => props => (
props.shouldSpin
? <View style={styles.container}>
<Text>Your fav spinner f.in. on data load.</Text>
<ActivityIndicator size="large" color="#0000ff" />
</View>
: <Component {...props} />
);
// Needs a HOC that provides prop shouldSpin (true/false)
You might want to ask a user to five star your app. You need a modal to do this:
const withModalOpener = Component => props => (
// Replace with your favourite Modal box implementation
<Component {...props} openModal={() => console.log('opening...')} />
);
Sometimes, modals should be intelligent enough to maintain their visibility, too:
// src/ Chapter_1/ Example_16_Useful_HOCs/ App.js
const withModalOpener = OriginalComponent => (
class ModalExample extends React.Component {
// Check this shorter way to init state
state = {
modalVisible: true,
};
setModalVisible(visible) {
this.setState({modalVisible: visible});
}
render() {
return (
// Replace with your favourite Modal box implementation
<View style={styles.container}>
<OriginalComponent
{...this.props}
openModal={() => this.setModalVisible(true)}
closeModal={() =>
this.setModalVisible(false)}
/>
<Modal
animationType="slide"
visible={this.state.modalVisible}
onRequestClose={() => {
alert('Modal has been closed.');
}}>
<View style={styles.container}>
<Text>Example modal!</Text>
<TouchableHighlight
onPress={() => {
this.setModalVisible(false);
}}>
<Text style={{fontSize: 30}}>
Hide Modal
</Text>
</TouchableHighlight>
</View>
</Modal>
</View>
);
}
}
);
In this example, we enriched the component with Modal. Modal can be opened or closed using the props that are named openModal and closeModal. The information regarding whether the modal is opened or closed is stored within a private state of the HOC and, in this example, is not exposed to the original component. Nice separation, right? This HOC is also reusable.
Time for your homework: how do we make Modal open along with the box show? You cannot change SomeComponent.
- LabVIEW入門與實戰(zhàn)開發(fā)100例
- 算法訓(xùn)練營:入門篇(全彩版)
- MATLAB 2020 從入門到精通
- Android 7編程入門經(jīng)典:使用Android Studio 2(第4版)
- Python數(shù)據(jù)分析從0到1
- 零基礎(chǔ)學(xué)Python網(wǎng)絡(luò)爬蟲案例實戰(zhàn)全流程詳解(高級進(jìn)階篇)
- 劍指MySQL:架構(gòu)、調(diào)優(yōu)與運(yùn)維
- Test-Driven Development with Django
- GameMaker Essentials
- 鴻蒙OS應(yīng)用編程實戰(zhàn)
- 區(qū)塊鏈國產(chǎn)化實踐指南:基于Fabric 2.0
- Simulation for Data Science with R
- QlikView Unlocked
- Mastering ASP.NET Core 2.0
- 零基礎(chǔ)學(xué)SQL(升級版)