Created
May 7, 2019 20:19
-
-
Save tswistak/240ad69369cc46e7425012f1a45ed882 to your computer and use it in GitHub Desktop.
InversifyJS with React Hooks, listing 1
This file contains hidden or 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 { useInjection } from './ioc.react'; | |
import { IProvider } from './providers'; | |
export const Hello: React.FC = () => { | |
const provider: IProvider<string>; // here we need to inject our provider | |
return ( | |
<h1>Hello {provider.provide()}!</h1> | |
); | |
}; |
This file contains hidden or 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 'reflect-metadata'; | |
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import { Hello } from './Hello'; | |
import { container } from './ioc'; | |
const App: React.FC = () => { | |
return ( | |
<div> | |
<Hello /> | |
</div> | |
); | |
}; | |
ReactDOM.render(<App />, document.getElementById('root')); |
This file contains hidden or 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 { Container } from 'inversify'; | |
import { IProvider, NameProvider } from './providers'; | |
export const container = new Container(); | |
container.bind<IProvider<string>>('nameProvider').to(NameProvider); |
This file contains hidden or 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 { injectable } from 'inversify'; | |
export interface IProvider<T> { | |
provide(): T; | |
} | |
@injectable() | |
export class NameProvider implements IProvider<string> { | |
provide() { | |
return 'World'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment