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

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.
主站蜘蛛池模板: 科技| 榕江县| 建昌县| 萍乡市| 济阳县| 浦东新区| 河曲县| 阳东县| 定边县| 嘉荫县| 泾源县| 孟连| 平利县| 陕西省| 通许县| 洪雅县| 阳曲县| 常德市| 余庆县| 青河县| 南澳县| 绥宁县| 嫩江县| 邯郸市| 孟村| 壤塘县| 库尔勒市| 盐源县| 怀来县| 巴南区| 康马县| 乐业县| 英吉沙县| 平阴县| 瑞昌市| 兴国县| 鱼台县| 南开区| 仁怀市| 江川县| 云梦县|