mobx-react-lite-inject
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

mobx-react-lite inject helper

Simple and convenient way to dependency injection

Observer work automatically

Example

Before inject:

import { observable, makeObservable } from 'mobx';
import { observer } from 'mobx-react-lite';

class Store {
	@observable public name: string = 'username';
	public constructor() { makeObservable(this); }
}
export default observer(({message}: {message: string}) =>
	<p>Hello {store.name}! {message}</p>
);

After inject:

import { observable, makeObservable } from 'mobx';
import { createContext } from 'react';
import inject from 'mobx-react-lite-inject';

class Store {
	@observable public name: string = 'username';
	public constructor() { makeObservable(this); }
}
const StoreContext createContext<Store>({} as Store);

export default inject([StoreContext], ([store], {message}: {message: string}) =>
	<p>Hello {store.name}! {message}</p>
);

Second store:

export default inject(
	[StoreContext, SecondContext],
	([store, second], {message}: {message: string}) => ...
);

Package Sidebar

Install

npm i mobx-react-lite-inject

Weekly Downloads

3

Version

1.0.1

License

MIT

Unpacked Size

3.76 kB

Total Files

5

Last publish

Collaborators

  • axgord