JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 435
  • Score
    100M100P100Q95800F
  • License MIT

Hierarchical Dependency Injection for React

Package Exports

  • react-ioc

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-ioc) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

React IoC

Hierarchical Dependency Injection for React

Build Status Coverage Status GitHub license npm version

Features

  • Hierarchical Injection
  • Can inject ES6 classes and React components
  • Can inject dependencies using React Hooks
  • Automatically calls .dispose() on created class instances when Recat unmouts Provider component
  • ES6, CommonJS and UMD bundles
  • Declarations for TypeScript and Flow
  • Type Safe even in JavaScript (with TypeScript --checkJs mode)
  • Tiny: only 1.5 KB (min+gzip)

Requirements: React 16.6 or greater, ES6 Map and Promise or polyfills.

Documentation

Example

import React, { Component } from "react";
import { provider, inject } from "react-ioc"
import { obserbvable, action } from "mobx";
import { observer } from "mobx-react";

class DataContext {
  users = observable.map<number, User>();
  posts = observable.map<number, Post>();
}

class PostService {
  @inject dataContext: DataContext;

  @action
  createPost(user: User) {
    const post = new Post({ id: uniqueId() });
    this.dataContext.posts.set(post.id, post);
    return post;
  }
}

@observer
class PostEditor extends Component {
  @inject postService: PostService;

  render() {
    // ...
  }
}

@provider(DataContext, PostService)
class App extends Component {
  render() {
    // ...
  }
}

@provider (alias @Provider)

TODO

@inject (alias @service, @Inject, @Service)

TODO

getInstance (alias getService)

TODO

getFunction (alias getComponent)

TODO

useInstance (alias useService) React Hook

import { useService, useServices } from "react-ioc";

const MyButton = props => {
  const myService = useService(MyService);

  return <button onClick={() => myService.doSomething()}>Ok</button>
}

const MyWidget = props => {
  const [fooService, barService] = useServices(FooService, BarService);

  return <div><MyButton /></div>
}

▲ back to top ▲


useFunction (alias useComponent) React Hook

TODO

Usage

> npm install --save react-ioc

UMD build

<script crossorigin src="https://unpkg.com/react-ioc/dist/index.umd.min.js"></script>
const { provider, inject } = window.ReactIoC;