Skip to content

Instantly share code, notes, and snippets.

@radzserg
Last active July 16, 2019 09:24
Show Gist options
  • Select an option

  • Save radzserg/4797e10423978c707cc16b648075cfed to your computer and use it in GitHub Desktop.

Select an option

Save radzserg/4797e10423978c707cc16b648075cfed to your computer and use it in GitHub Desktop.
// DIContext.ts
import React from "react";
import DIContainer, { IDIContainer } from "rsdi";
const DIContext = React.createContext<IDIContainer>(new DIContainer());
export default DIContext;
// App.ts
// build container only once
const diContainer: IDIContainer = configureDIContainer();
class App extends PureComponent {
render() {
// now we can get values from container
const apolloClient = diContainer.get<ApolloClient<any>>(APP_DEP.APOLLO_CLIENT);
const reduxStore = diContainer.get<Store>(APP_DEP.REDUX_STORE);
const browserHistory = diContainer.get<History>(APP_DEP.BROWSER_HISTORY);
return (
<ErrorBoundary>
<ApolloProvider client={apolloClient}> {/* we still need these contexts, but that's fine */}
<Provider store={reduxStore}>
<DIContext.Provider value={diContainer}> {/* we will need our own context */}
<Router history={browserHistory}>
<Layout>
<Switch>
<Route path="/" component={HomePage} exact={true} />
</Switch>
</Layout>
</Router>
</DIContext.Provider>
</Provider>
</<ApolloProvider>
</ErrorBoundary>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment