ditox-react
Version:
Dependency injection container for React.js
31 lines • 1.01 kB
JavaScript
import { bindModule } from 'ditox';
import React, { useCallback } from 'react';
import { DependencyContainer, } from './DependencyContainer';
/**
* Binds the module to a new dependency container.
*
* If a parent container is exist, it is connected to the current one by default.
*
* @param params.module - Module declaration for binding
* @param params.scope - Optional scope for binding: `singleton` (default) or `scoped`.
*
* @example
*
* ```tsx
* const LOGGER_MODULE: ModuleDeclaration<LoggerModule> = {
*
* function App() {
* return (
* <DependencyModule module={LOGGER_MODULE}>
* <NestedComponent />
* </DependencyModule>
* );
* }
* ```
*/
export function DependencyModule(params) {
const { children, module, scope } = params;
const binder = useCallback((container) => bindModule(container, module, { scope }), [module, scope]);
return React.createElement(DependencyContainer, { binder: binder }, children);
}
//# sourceMappingURL=DependencyModule.js.map