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

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.
主站蜘蛛池模板: 肇州县| 丹东市| 老河口市| 绵阳市| 淳安县| 西藏| 马尔康县| 沙坪坝区| 色达县| 右玉县| 井冈山市| 兴业县| 舒兰市| 宜良县| 南京市| 西丰县| 夹江县| 旬阳县| 滁州市| 贡山| 靖边县| 淮滨县| 台安县| 镇赉县| 申扎县| 宁强县| 黔西县| 滨海县| 澄迈县| 安福县| 绥宁县| 昌乐县| 平定县| 高阳县| 桑日县| 临高县| 遂昌县| 安国市| 东光县| 铅山县| 乡宁县|