Skip to content

Instantly share code, notes, and snippets.

@lkostrowski
Last active January 29, 2019 22:05
Show Gist options
  • Save lkostrowski/d69be1c846f7ce44ad39a2df61e646fe to your computer and use it in GitHub Desktop.
Save lkostrowski/d69be1c846f7ce44ad39a2df61e646fe to your computer and use it in GitHub Desktop.
React DI Poc
import React from 'react';
import { DependencyContainer, ProvidersMap } from 'some-react-di';
const providers = new ProvidersMap([SomeService, {token: AnotherService, provide: new MockService()}]);
providers.bind('string-token').to('some-value');
export const App = () =>
(
<DependencyContainer providersMap={providers}>
<SomeContainerComponentA />
<SomeContainerComponentB />
</DependencyContainer/>
)
import React from 'react';
import { useProvider } from 'some-react-di';
export const SomeContainerComponentA = () => {
const service: SomeService = useProvider(SomeService);
return <div>{service.foo}</div>
}
import React from 'react';
import { withProvider } from 'some-react-di';
const SomeContainerComponentBpure = ({service, staticValue}: {service: AnotherService, staticValue: string}) => (
<div>{service.someMockData}, {staticValue}</div>
)
export const SomeContainerComponentB = compose(
withProvider('service', AnotherService),
withProvider('staticValue', 'string-token'),
)(SomeContainerComponentBpure);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment