# thinjector

> Minimalistic, super lightweight React service injection container extremely easy to use.

Latest version **1.0.4** (published 2022-05-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install thinjector
pnpm add thinjector
yarn add thinjector
bun add thinjector
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2022-05-15 |
| First published | 2021-05-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=6.0.0 |
| Dependencies | 0 |
| Unpacked size | 24.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Cesar Martinez |
| Maintainers | cesarj41 |

## Links

- npm: https://www.npmjs.com/package/thinjector
- npm.io page: https://npm.io/package/thinjector

## Recent versions

- 1.0.4 (latest) — 2022-05-15
- 1.0.2 — 2021-05-31
- 1.0.1 — 2021-05-30
- 1.0.0 — 2021-05-30

## README

# Thinjector (React)

Minimalistic, super lightweight React service injection container extremely easy to use.

## Why
It's important to decouple your ui components from your services (fetching logic, biz logic, etc..) but the solutions out there are either using heavy weight state management tools (redux + redux saga, mobx, recoil, etc..) or full fledge dependency containers (inversifyjs, 
tsyringe) which for a lot of use cases are overkill so looking for lightweight solutions the ones I found which are awesome but didn't feel comfortable with the api, so I came with this very small solution.
### Features
- Built with Typescript.
- A hook for accessing services.

### Installation

```bash
npm install thinjector
```

### Usage

Set up your service container, create the file where you wish in your project folder structure, I will put it on services/index.ts.
```typescript
import { createServiceContainer } from 'thinjector'

// Service structure is up to you, this is just a simple example
interface UserService {
    login: VoidFunction
}
export interface IServices {
    userService: UserService
}

const services: IServices = {
    userService: {
        login: () => console.log('signing in....'),
    }
}

export const container =
  createServiceContainer<IServices>(services);
```

And now lets configure the service container provider at the root of your React app, normally App.tsx

```typescript
import React from "react"
import container from "./services"

const { ServiceProvider } = container;

const App = () => {
  return (
    <ServiceProvider>
      {/* Your app.... */}
    </ServiceProvider>
  );
};

export default App;
```
And ... that's it, you can start accessing your services any part down the tree !.
## Injecting services examples
### Using useService hook !
```typescript
import React from "react"
import container from "./services"

const { useService } = container;

const DemoPage = () => {
  const { userService } = useService(); // WoW, just like that
  return (
    <div onClick={() => userService.login()}>
      Demo Page
    </div>
  );
};

export default DemoPage;
```

Not a hook fan ? still using class components ?, don't worry, we got you covered with a HOC too !
### Using withService HOC !
```typescript
import React from "react"
import container, {IServices} from "./services"

const { withService } = container;

type Props = {
    service: IServices
}
const DemoPage = ({service}: Props) => {
  return (
    <div onClick={() => service.userService.login()}>
      Demo Page
    </div>
  );
};

export default withService(DemoPage);
```
Don't like the idea of injecting all services and prefer a solution to specify which services or functions you want like a redux mapToProps thing ? don't say no more:
### Using inject HOC !
```typescript
import React from "react"
import container, {IServices} from "./services"

const { inject } = container;

type Props = {
    login: IServices['userService']['login']
}
const DemoPage = ({login}: Props) => {
  return (
    <div onClick={() => login()}>
      Demo Page
    </div>
  );
};

export default inject(DemoPage, (services) => ({
    login: services.userService.login
}));
```

---
_Source: https://npm.io/package/thinjector · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
