Skip to content

Instantly share code, notes, and snippets.

@DScheglov
Last active October 21, 2020 07:00
Show Gist options
  • Save DScheglov/ef8cdc22a54ea535a6b31ddff3fce78f to your computer and use it in GitHub Desktop.
Save DScheglov/ef8cdc22a54ea535a6b31ddff3fce78f to your computer and use it in GitHub Desktop.
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