-
Notifications
You must be signed in to change notification settings - Fork 630
/
Copy pathHOC.js
34 lines (28 loc) · 924 Bytes
/
HOC.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
* Copyright (c) 2018-present, Evgeny Nadymov
*
* This source code is licensed under the GPL v.3.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import React from 'react';
export function compose(...funcs) {
return funcs.reduce((a, b) => (...args) => a(b(...args)), arg => arg);
}
export function withSaveRef() {
return Component => {
return React.forwardRef((props, ref) => <Component {...props} forwardedRef={ref} />);
};
}
export function withRestoreRef() {
return Component => {
return class extends React.Component {
render() {
const { forwardedRef, ...rest } = this.props;
return <Component {...rest} ref={forwardedRef} />;
}
};
};
}
export function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || 'Component';
}