From 39323369bbdeddfde50aca313d91e93b29ff333e Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Mon, 22 Jul 2019 21:33:48 -0600 Subject: [PATCH] Use hooks for context --- client/state/reactContext.tsx | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/client/state/reactContext.tsx b/client/state/reactContext.tsx index b41cc94..2c7f774 100644 --- a/client/state/reactContext.tsx +++ b/client/state/reactContext.tsx @@ -33,20 +33,16 @@ export function ConsumeState({ children }: ConsumeStateProps) { export function injectState

( Component: React.ComponentType

-): React.ComponentClass> { - return class extends React.Component> { - render() { - const consumeState = (state: AppState | null) => { - if (state == null) { - throw new Error( - "Component with injectState must be mounted inside ProvideState" - ); - } - // tslint:disable-next-line:no-object-literal-type-assertion - const allProps: Readonly

= {...this.props, appState: state} as Readonly

; - return ; - }; - return {consumeState}; +): React.FunctionComponent> { + return function InjectState(props) { + const state = React.useContext(StateContext); + if (state == null) { + throw new Error( + "Component with injectState must be mounted inside ProvideState" + ); } - }; + // tslint:disable-next-line: no-object-literal-type-assertion + const allProps: Readonly

= {...props, appState: state} as Readonly

; + return ; + } }