Last active
December 25, 2020 09:49
-
-
Save lovetingyuan/26e9b65ff5a6f83c8893b8d1cf154a68 to your computer and use it in GitHub Desktop.
Revisions
-
lovetingyuan revised this gist
Dec 25, 2020 . 1 changed file with 12 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,29 +5,31 @@ interface ContainerProviderProps<State = void> { children: React.ReactNode } const usecontext = React.useContext export default function createContainer<Value, State = void>( usehook: (initialState?: State) => Value, ) { const Context = React.createContext<Value | null>(null) function Container(props: ContainerProviderProps<State>): JSX.Element function Container(): Value function Container(props?: ContainerProviderProps<State>): Value | JSX.Element { if (props) { const value = usehook(props.initialState) return React.createElement(Context.Provider, { value }, props.children) } const value = usecontext(Context) if (value === null) { throw new Error('Component must be wrapped with container component returned by `createContainer`.') } return value } if (usehook.name) { Object.defineProperty(Container, 'name', { value: usehook.name, configurable: true }) } return Container } -
lovetingyuan revised this gist
Dec 25, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -19,7 +19,7 @@ export default function createContainer<Value, State = void>( } const value = React.useContext(Context) if (value === null) { throw new Error('Component must be wrapped with container component returned by `createContainer`.') } return value } -
lovetingyuan revised this gist
Dec 23, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -19,7 +19,7 @@ export default function createContainer<Value, State = void>( } const value = React.useContext(Context) if (value === null) { throw new Error('Component must be wrapped with container component.') } return value } -
lovetingyuan renamed this gist
Dec 23, 2020 . 1 changed file with 7 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,27 +1,27 @@ import React from 'react' interface ContainerProviderProps<State = void> { initialState?: State children: React.ReactNode } export default function createContainer<Value, State = void>( useHook: (initialState?: State) => Value, ) { const Context = React.createContext<Value | null>(null) function container(props: ContainerProviderProps<State>): JSX.Element function container(): Value function container(props?: ContainerProviderProps<State>): Value | JSX.Element { if (props) { const value = useHook(props.initialState) return React.createElement(Context.Provider, { value }, props.children) } const value = React.useContext(Context) if (value === null) { throw new Error('Component must be wrapped with <Container.Provider>') } return value } if (useHook.name) { Object.defineProperty(container, 'name', { -
lovetingyuan created this gist
Dec 23, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,33 @@ import React from 'react' interface ContainerProviderProps<State = void> { initialState?: State children: React.ReactNode } export default function createContainer<Value, State = void>( useHook: (initialState?: State) => Value, ) { const Context = React.createContext<Value | null>(null) function container(props: ContainerProviderProps<State>): JSX.Element function container(): Value function container (props?: ContainerProviderProps<State>): Value | JSX.Element { if (props) { const value = useHook(props.initialState) return <Context.Provider value={value}>{props.children}</Context.Provider> } const value = React.useContext(Context) if (value === null) { throw new Error('Component must be wrapped with <Container.Provider>') } return value } if (useHook.name) { Object.defineProperty(container, 'name', { value: useHook.name, configurable: true }) } return container }