Last active
October 21, 2020 07:00
-
-
Save DScheglov/ef8cdc22a54ea535a6b31ddff3fce78f to your computer and use it in GitHub Desktop.
This file contains 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 characters
import React from 'react'; | |
import { compose } from "...somewhere..."; | |
export const diContext = React.createContext({ | |
container: {}, | |
run: () => {}, | |
)); | |
export const { Consumer } = diContext; | |
export function Provider({ children, container }) { | |
const run = React.useCallback( | |
fn => fn(container, run), | |
[container], | |
); | |
return <diContext.Provider value={{ container, run }}>{children}</diContext.Provider>; | |
}; | |
export const useInjected = () => React.useContext(diContext).container; | |
export const useRun = fn => { | |
const { run } = React.useContext(diContext); | |
return React.useMemo(() => compose(run, fn), [fn]); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment