-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectContext.tsx
24 lines (19 loc) · 966 Bytes
/
connectContext.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { FC, ReactElement, useContext } from 'react';
import * as React from 'react';
export function connectContext<TContext>(context: React.Context<TContext>):
<TProps extends TContextProps, TContextProps>
(Cmp: FC<TProps>, mapContextToProps: (context: TContext) => TContextProps) => FC<Omit<TProps, keyof TContextProps>> {
return function
<TProps extends TContextProps, TContextProps>
(Cmp: FC<TProps>, mapContextToProps: (context: TContext) => TContextProps): FC<Omit<TProps, keyof TContextProps>> {
return function(props: Omit<TProps, keyof TContextProps>): ReactElement {
const contextToAssign: TContext = useContext(context);
const dependencies: TContextProps = mapContextToProps(contextToAssign);
const propsWithFields: TProps = {
...props,
...dependencies
} as TProps;
return <Cmp {...propsWithFields}/>;
};
}
}