# okto-core

> ## How it works? `okto-js` brings `IoC` functionality into React.

Latest version **0.1.6** (published 2020-02-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install okto-core
pnpm add okto-core
yarn add okto-core
bun add okto-core
```

## Health

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

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

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.6 |
| Published | 2020-02-24 |
| First published | 2020-01-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 14.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | pirropirro |
| Maintainers | pirropirro |

## Links

- npm: https://www.npmjs.com/package/okto-core
- Repository: https://github.com/pirropirro/okto-js
- Homepage: https://github.com/pirropirro/okto-js#readme
- Issues: https://github.com/pirropirro/okto-js/issues
- npm.io page: https://npm.io/package/okto-core

## Dependencies (4)

- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [inversify](https://npm.io/package/inversify.md) ^4.3.0
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) 0.1.8
- [inversify-inject-decorators](https://npm.io/package/inversify-inject-decorators.md) ^3.1.0

## Recent versions

- 0.1.6 (latest) — 2020-02-24
- 0.1.5 — 2020-01-14
- 0.1.4 — 2020-01-09
- 0.1.3 — 2020-01-04
- 0.1.2 — 2020-01-04
- 0.0.8-beta.3 — 2020-01-04
- 0.0.8-beta.2 — 2020-01-04

## README

# okto-js

## How it works? 
`okto-js` brings `IoC` functionality into React.

## How to use it

- Create a service 
```typescript
import { inject, injectable } from "inversify";

export const IConfigRetriever = Symbol("IConfigRetriever");
export interface IConfigRetriever {
  retrieve(): any;
}

@injectable()
export class ConfigRetriever extends IConfigRetriever{
  constructor(@inject("Config") private config: any) {}

  public retrieve(): any {
    return this.config;
  }
}
```

- Create a module and register the service in it.
```typescript
import { interfaces } from "inversify";
import { IModule } from "okto-js";

import { IConfigRetriever,  ConfigRetriever } from './ConfigRetriever';

export class ConfigModule implements IModule, IViewModelsModule {
 modules(container: interfaces.Container): void { 
    container.bind<IConfigRetriever>(IConfigRetriever).to(ConfigRetriever);
  }
}
```


- Create a component where use the service
```tsx
import { useState, useEffect } from "react";
import { useContainer } from "okto-js";

import { IConfigRetriever } from './ConfigRetriever';

export function Config = () => {
  const retriever = useContainer<IConfigRetriever>(IConfigRetriever);
  const [config, setConfig] = useState<any>({});

  useEffect(() => setConfig(retriever.retrieve()), [retriever]);

  return <div>{JSON.stringify(config)}</div>
}
```

- Register and run the app
```tsx
import "reflect-metadata";
import * as React from "react";
import { Application } from "okto-js";

import { Config } from "./Config";
import { ConfigModule } from "./ConfigModule";

const app = new Application();
app.register(new ConfigModule());

// Here you can add as many Module as you want :)

app.run(() => <Config />);
```

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