npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-ioc-immer

v0.1.19

Published

Hierarchical Dependency Injection for React with Immutable State Services

Downloads

440

Readme

React IoC with Immer

**Hierarchical Dependency Injection for React With Immutable State Management **

This is a fork of awesome project React-Ioc

Main goals

  • use OOB React Context abilities for propagating changes child compoenents instead of using MobX approach

Features

  • Hierarchical Dependency Injection
  • Can inject dependencies using React Hooks
  • Automatically calls .dispose() on created class instances when React unmount Provider component
  • Can work without decorators
  • Supports lazy service registration with code splitting
  • ES6, CommonJS and UMD bundles
  • Declarations for TypeScript and Flow
  • Rewritten on TypeScript

Differenece with original project React-Ioc

  • Instead of MobX using immutable state manager based on Immer

Requirements: React 16.6 or greater, ES6 Map or Map polyfill.

Documentation

Example

import React from 'react';
import { provider, inject, action, ImmutableService, store } from 'react-ioc-immer';

class LoginService extends ImmutableService {
	@store() users: User[];
	private someVar: number;
	@store() complexObject: {
		title: string;
		items: {}[];
	};
}

class PostService {
	@inject dataContext: DataContext;

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


class PostEditor extends React.Component {
	@inject postService: PostService;

	render() {
		// ...
	}
}

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

@provider (alias @Provider)

HOC (or decorator) that registers dependencies in scope of wrapped component.

import { provider, toClass, toFactory, toValue, toExisting } from 'react-ioc';

@provider(
	DataContext, // bind DataContext to self
	[IFooService, FooService][(IBarService, toClass(BarService))][(IBazService, toValue({ baz: 123 }))][ // bind IFooService to FooService // bind IBarService to BarService // bind IBazService to static value
		// bind MobxStore to factory with dependencies
		(MobxStore, toFactory([IFooService, IBarService], (fooService, barService) => MobxStore.create(fooService, barService)))
	][
		// bind IObsoleteService to already registered IFooService instance
		(IObsoleteService, toExisting(IFooService))
	]
)
class App extends React.Component {
	render() {
		// ...
	}
}

Providers can be nested:

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

@provider(UserService)
class HomePage extends React.Component {
	render() {
		// ...
	}
}

Also Provider component has static register() function, for imperative dependencies registration:

// App.jsx
import { provider, toClass } from 'react-ioc';

class App extends React.Component {}

export default provider()(App);
// somewhere else
import App from './App';

App.register(FooService, [BarService, toClass(BarService)]);

▲ back to top ▲

@registerIn (alias @RegisterIn)

Class decorator for lazy service registration in Provider. Accepts lambda that returns some Proveider component.

// ./services/LazyService.js
import { registerIn } from 'react-ioc';
import App from '../components/App';

@registerIn(() => App)
export class LazyService {}
// ./components/LazyWidget.jsx
import { inject } from 'react-ioc';
import { LazyService } from '../services/LazyService';

export default class LazyWidget extends React.Component {
	@inject lazyService: LazyService;
}
// ./components/App.jsx
import { provider } from 'react-ioc';
const LazyWidget = React.lazy(() => import('./LazyWidget'));

@provider()
export default class App extends React.Component {
	render() {
		return (
			<React.Suspense fallback={<div>Loading...</div>}>
				<LazyWidget />
			</React.Suspense>
		);
	}
}

Also, is can accept binding as second argument:

// ./services/LazyService.js
import { registerIn, toClass } from 'react-ioc';
import App from '../components/App';

interface LazyService {
	method(): void;
}

class LazyServiceImpl implements LazyService {
	// ...
}

@registerIn(() => App, toClass(LazyServiceImpl))
export class LazyService {}

▲ back to top ▲

@inject (alias @Inject)

Property decorator for property dependency injection.

Can use dependency types from Reflect Metadata (with TypeScript --emitDecoratorMetadata):

import { inject } from 'react-ioc';

class FooService {
	@inject barService: BarService;
}

class MyComponent extends React.Component {
	@inject fooService: FooService;
	@inject barService: BarService;
	// ...
}

Or manually specified dependencies:

import { inject } from 'react-ioc';

class FooService {
	@inject(BarService) barService;
}

class MyComponent extends React.Component {
	@inject(FooService) fooService;
	@inject(BarService) barService;
	// ...
}

If you want to use dependency in React.Component constructor you should pass context argument to super():

import React from 'react';
import { inject } from 'react-ioc';

class MyComponent extends React.Component {
	@inject fooService: FooService;

	constructor(props, context) {
		super(props, context);
		this.fooService.doSomething();
	}
}

▲ back to top ▲

inject

Utility function for property or constructor dependency injection. Note, that for React Components we should explicitely define static contextType = InjectorContext (unlike with @inject decorator).

Property Injection:

import { inject, InjectorContext } from 'react-ioc';

class FooService {
	barService = inject(this, BarService);
}

class MyComponent extends React.Component {
	fooService = inject(this, FooService);
	barService = inject(this, BarService);

	static contextType = InjectorContext;
}

Constructor Injection:

import { inject } from 'react-ioc';

class OtherService {
	constructor(fooService, barService) {
		this.fooService = fooService || inject(this, FooService);
		this.barService = barService || inject(this, BarSerivce);
	}
}

▲ back to top ▲

useInstance React Hook

import { useInstance, useInstances } from 'react-ioc';

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

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

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

	return (
		<div>
			<MyButton />
		</div>
	);
};

▲ back to top ▲

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;