Last active
January 29, 2019 22:05
-
-
Save lkostrowski/d69be1c846f7ce44ad39a2df61e646fe to your computer and use it in GitHub Desktop.
React DI Poc
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 { 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/> | |
) |
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 { useProvider } from 'some-react-di'; | |
export const SomeContainerComponentA = () => { | |
const service: SomeService = useProvider(SomeService); | |
return <div>{service.foo}</div> | |
} |
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 { 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