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

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.
主站蜘蛛池模板: 武威市| 益阳市| 韶山市| 黑水县| 乐昌市| 普兰店市| 新疆| 菏泽市| 安陆市| 平潭县| 皮山县| 永寿县| 神农架林区| 连平县| 平江县| 延长县| 抚州市| 美姑县| 习水县| 深圳市| 通化县| 正蓝旗| 临清市| 华安县| 盐城市| 莱州市| 湖州市| 宣化县| 正镶白旗| 阳朔县| 夹江县| 云和县| 洛浦县| 社会| 花垣县| 宁蒗| 涪陵区| 安义县| 兴文县| 包头市| 蒙城县|