- Hands-On Design Patterns with React Native
- Mateusz Grzesiukiewicz
- 180字
- 2021-08-13 15:12:57
HOC composition
The primary reason to create HOCs it to have the ability to compose the features they provide.
Look at the problem from the previous section again. What if we could delegate work to another HOC? For instance, having a mapper HOC called mapPropNames, you can compose it with our previous HOC like this:
makeExpandable(mapPropNames(SomeSection));
Here is the implementation of mapPropNames:
// src/ Chapter_1/ Example_15_HOC_Composition/ App.js
const mapPropNames = (Component) => (props) => (
<Component
{...props}
isVisible={props.isExpanded}
showHideBox={props.expandOrCollapse}
/>
);
Nice and quick, isn't it? This is a common pattern and is also used when working with backend data sent as JSON. It may adapt the data format to our representation on the frontend layer. As you see, we can employ this great idea when working with HOCs as well!
If you come from an object-oriented background, please notice that the HOC pattern is very similar to the decorator pattern. The decorator, however, also relies on inheritance and needs to implement the interface that it decorates.
Please check https://en.wikipedia.org/wiki/Decorator_pattern for examples.
You can also compose decorators. It works in a similar way.
Please check https://en.wikipedia.org/wiki/Decorator_pattern for examples.
You can also compose decorators. It works in a similar way.
推薦閱讀
- SPSS數(shù)據(jù)挖掘與案例分析應(yīng)用實(shí)踐
- HornetQ Messaging Developer’s Guide
- Vue.js設(shè)計(jì)與實(shí)現(xiàn)
- Learning ROS for Robotics Programming(Second Edition)
- Apache Spark 2.x Machine Learning Cookbook
- Microsoft Dynamics 365 Extensions Cookbook
- Functional Programming in JavaScript
- 高級(jí)C/C++編譯技術(shù)(典藏版)
- Microsoft System Center Orchestrator 2012 R2 Essentials
- 軟件架構(gòu):Python語(yǔ)言實(shí)現(xiàn)
- C#程序設(shè)計(jì)基礎(chǔ):教程、實(shí)驗(yàn)、習(xí)題
- Instant Lucene.NET
- Microsoft Dynamics AX 2012 R3 Financial Management
- C++寶典
- Node.js開發(fā)指南