@proxydi/react
Version:
React wrapper for the ProxyDi library
75 lines (51 loc) • 2.24 kB
Markdown
[](https://coveralls.io/github/proxy-di/proxydi-react?branch=coverage-badge)
React wrapper for [ProxyDi](https://www.npmjs.com/package/proxydi) library
<img src="https://github.com/proxy-di/proxydi-react/blob/main/assets/ProxyDiReactLogo.png?raw=true" width="196">
Install the library with its core dependency:
```shell
npm install @proxydi/react proxydi
```
In your main application file (e.g., `index.tsx`) wrap your components by [< ProxyDiProvider />](https://proxy-di.github.io/proxydi-react/functions/ProxyDiProvider.html) to give them access to dependencies in [ProxyDiContainer](https://proxy-di.github.io/proxydi/classes/ProxyDiContainer.html)
```tsx
import React from 'react';
import ReactDOM from 'react-dom';
import { ProxyDiProvider } from '@proxydi/react';
import { ProxyDiContainer } from 'proxydi';
import App from './App';
import MyService from './MyService';
const initAppDependencies = (container: ProxyDiContainer) => {
container.register(MyService, 'My-Service');
};
ReactDOM.render(
<ProxyDiProvider init={initAppDependencies}>
<App />
</ProxyDiProvider>,
document.getElementById('root'),
);
```
In your React component, access your services via the [useProxyDi()](https://proxy-di.github.io/proxydi-react/functions/useProxyDi.html) hook.
```tsx
import React from 'react';
import { useProxyDi } from '@proxydi/react';
import MyService from './MyService';
const MyComponent = () => {
const myService = useProxyDi<MyService>('My-Service');
return (
<div>
<h1>Data from MyService:</h1>
<p>{myService.getData()}</p>
</div>
);
};
export default MyComponent;
```
You can use nested `<ProxyDiProvider />` instances when you need a [child container](https://proxy-di.github.io/proxydi/#hierarchy-of-containers).
Contribution documentation is not ready yet but is planned. Feel free to contribute even now though! :)
This project is licensed under the terms of the MIT License. See the [LICENSE](./LICENSE) file for details.