Installations
npm install inversify-react
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
21.7.3
NPM Version
10.5.0
Score
90.3
Supply Chain
97.8
Quality
77.7
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (97.56%)
JavaScript (2.44%)
validate.email 🚀
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Developer
Kukkimonsuta
Download Statistics
Total Downloads
1,660,832
Last Day
2,896
Last Week
14,223
Last Month
59,100
Last Year
637,946
GitHub Statistics
Apache-2.0 License
165 Stars
73 Commits
9 Forks
7 Watchers
2 Branches
4 Contributors
Updated on Mar 11, 2025
Bundle Size
6.52 kB
Minified
2.23 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.2.0
Package Id
inversify-react@1.2.0
Unpacked Size
60.72 kB
Size
17.50 kB
File Count
12
NPM Version
10.5.0
Node Version
21.7.3
Published on
Dec 27, 2024
Oops! Something went wrong.
inversify-react
Hooks and decorators for InversifyJS + React.
Table of Contents
- Motivation
- Installation
- Usage overview
- Provider
- React hooks
- React component decorators (for classes)
- Notes, tips
Motivation
TL;DR:
- InversifyJS, as IoC container, is great for automatic DI
- use it also in React
Installation
npm install --save inversify-react
yarn add inversify-react
...on top of your project with other modules already installed and configured
react
inversify
reflect-metadata
Keep in mind that Inversify uses decorators, which requires some setup for your build process.
Read more about decorators:
- https://github.com/inversify/InversifyJS#installation
- https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy
- https://www.typescriptlang.org/docs/handbook/decorators.html
inversify-react
also uses decorators, but only when used in Class Components.
Usage overview
Usage is pretty similar to React Context.
-
Wrap React component tree with
Provider
andContainer
frominversify-react
– just like React Context.Provider1import { Provider } from 'inversify-react'; 2... 3 4<Provider container={myContainer}> 5 ... 6</Provider>
-
Use dependencies from that container in child components
1import { resolve, useInjection } from 'inversify-react'; 2... 3 4// In functional component – via hooks 5const ChildComponent: React.FC = () => { 6 const foo = useInjection(Foo); 7 ... 8}; 9 10// or in class component – via decorated fields 11class ChildComponent extends React.Component { 12 @resolve 13 private readonly foo: Foo; 14 ... 15}
Provider
1<Provider container={myContainer}> 2 ... 3</Provider>
- provides contextual IoC container for children, similar to React Context.Provider
- can automatically establish hierarchy of containers in React tree when you use multiple Providers (e.g. in a big modular app)
- props:
container
- container instance or container factory functionstandalone
- (optional prop,false
by default) whether to skip hierarchy of containers. Could be useful if you already control container hierarchy and would like to ignore React-tree-based hierarchy.
1import * as React from 'react'; 2import { Container } from 'inversify'; 3import { Provider } from 'inversify-react'; 4 5// in functional component 6const AppOrModuleRoot: React.FC = () => { 7 return ( 8 <Provider container={() => { 9 const container = new Container(); 10 container.bind(Foo).toSelf(); 11 container.bind(Bar).toSelf(); 12 return container; 13 }}> 14 {/*...children...*/} 15 </Provider> 16 ); 17}; 18 19// or class component 20class AppOrModuleRoot extends React.Component { 21 22 // you can create and store container instance explicitly, 23 // or use factory function like in functional component example above 24 private readonly container = new Container(); 25 26 constructor(props: {}, context: {}) { 27 super(props, context); 28 29 const { container } = this; 30 container.bind(Foo).toSelf(); 31 container.bind(Bar).toSelf(); 32 } 33 34 render() { 35 return ( 36 <Provider container={this.container}> 37 {/*...children...*/} 38 </Provider> 39 ); 40 } 41}
React hooks
useInjection
1const foo = useInjection(Foo);
- very similar to React.useContext hook, resolves dependency by id
useOptionalInjection
1// e.g. Foo and Bar are not bound 2const foo = useOptionalInjection(Foo); // will return undefined 3// or 4const bar = useOptionalInjection(Bar, () => 'defaultBar'); // will return 'defaultBar'
- resolves optional dependency
- default value can be defined via lazy resolving function (2nd argument)
That function conveniently receives container as argument, so you could instantiate your default using container (e.g. if it has dependencies)1const foo = useOptionalInjection(Foo, () => myDefault); 2// foo === myDefault 3// ^ Foo | typeof myDefault
1const foo = useOptionalInjection(Foo, container => container.resolve(X));
useContainer
1const container = useContainer(); 2// or 3const foo = useContainer(container => container.resolve(Foo));
- low-level hook, resolves container itself
- has overload with callback to immediately resolve value from container, so could be used for more exotic API, e.g. named or tagged bindings
useAllInjections
1const bars = useAllInjections(Bar);
- @see multi-inject
For more examples, please refer to tests: test/hooks.tsx
React component decorators (for classes)
@resolve
1@resolve 2foo: Foo; 3 4// or strict and semantic, see tips below 5@resolve 6private readonly foo!: Foo;
- resolves service from container
- requires
reflect-metadata
andemitDecoratorMetadata
1// or pass service identifier explicitly 2// e.g. if you deal with interfaces and/or don't want to use field type (via reflect-metadata) 3@resolve(IFooServiceId) 4private readonly foo!: IFoo;
@resolve.optional
1@resolve.optional 2private readonly foo?: Foo;
- tries to resolve service from container, but returns
undefined
if service cannot be obtained - requires
reflect-metadata
andemitDecoratorMetadata
@resolve.optional(serviceId, defaultValue?)
- obtains service from container passed down in the React tree, returns
defaultValue
if service cannot be obtained
1class ChildComponent extends React.Component { 2 @resolve 3 private readonly foo!: Foo; 4 5 @resolve(Bar) 6 private readonly bar!: Bar; 7 8 @resolve.optional(Baz) 9 private readonly opt?: Baz; 10 11 ... 12} 13 14// you can also use dependency in constructor, 15// just don't forget to call super with context 16// @see https://github.com/facebook/react/issues/13944 17constructor(props: {}, context: {}) { 18 super(props, context); 19 console.log(this.foo.name); 20}
@resolve.all
1@resolve.all('Foo') 2private readonly foo!: Foo[];
- tries to resolve all services from container, fails if no services are bound to given service identifier
- requires
reflect-metadata
andemitDecoratorMetadata
, but cannot be used without explicitly specifying service identifier
@resolve.all(serviceId)
- obtains services from container passed down in the React tree
1class ChildComponent extends React.Component { 2 @resolve.all(Baz) 3 private readonly all!: Baz[]; 4 5 ... 6}
@resolve.optional.all
1@resolve.optional.all('Foo') 2private readonly foo!: Foo[];
- tries to resolve all services from container, returns empty array if none are registered
- requires
reflect-metadata
andemitDecoratorMetadata
, but cannot be used without explicitly specifying service identifier
@resolve.optional.all(serviceId)
- obtains services from container passed down in the React tree
1class ChildComponent extends React.Component { 2 @resolve.optional.all(Baz) 3 private readonly allorempty!: Baz[]; 4 5 ... 6}
Notes, tips
-
[TypeScript tip]
private readonly
for@resolve
-ed fields is not required, but technically it's more accurate, gives better semantics and all. -
[TypeScript tip]
!
for@resolve
-ed fields is needed for strictPropertyInitialization / strict flags (which are highly recommended). -
[InversifyJS tip] If you're binding against interface, then it might be more comfortable to collocate service identifier and type. With typed service identifier you get better type inference and less imports. Way better DX compared to using strings as identifiers.
1export interface IFoo { 2 // ... 3} 4export namespace IFoo { 5 export const $: interfaces.ServiceIdentifier<IFoo> = Symbol('IFoo'); 6}
1container.bind(IFoo.$).to(...); 2// ^ no need to specify generic type, 3// type gets inferred from explicit service identifier
1// in constructor injections (not in React Components, but in services/stores/etc) 2constructor(@inject(IFoo.$) foo: IFoo) 3 4// in React Class component 5@resolve(IFoo.$) 6private readonly foo!: IFoo; // less imports and less chance of mix-up 7 8// in functional component 9const foo = useInjection(IFoo.$); // inferred as IFoo 10

No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: Apache License 2.0: LICENSE:0
Reason
4 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 4
Reason
Found 3/28 approved changesets -- score normalized to 1
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 5 are checked with a SAST tool
Reason
51 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-6chw-6frg-f759
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-x9w5-v3q2-3rhw
- Warn: Project is vulnerable to: GHSA-w8qv-6jwh-64r5
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-vh7m-p724-62c2
- Warn: Project is vulnerable to: GHSA-r9p9-mrjm-926w
- Warn: Project is vulnerable to: GHSA-434g-2637-qmqr
- Warn: Project is vulnerable to: GHSA-49q7-c7j4-3p7m
- Warn: Project is vulnerable to: GHSA-977x-g7h5-7qgw
- Warn: Project is vulnerable to: GHSA-f7q4-pwc6-w24p
- Warn: Project is vulnerable to: GHSA-fc9h-whq2-v747
- Warn: Project is vulnerable to: GHSA-vjh7-7g9h-fjfh
- Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3
- Warn: Project is vulnerable to: MAL-2023-462
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp
- Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq
- Warn: Project is vulnerable to: GHSA-3rfm-jhwj-7488
- Warn: Project is vulnerable to: GHSA-hhq3-ff78-jv3g
- Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-fhjf-83wg-r2j9
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-h9rv-jmmf-4pgx
- Warn: Project is vulnerable to: GHSA-hxcc-f52p-wc94
- Warn: Project is vulnerable to: GHSA-4g88-fppr-53pp
- Warn: Project is vulnerable to: GHSA-4jqc-8m5r-9rpr
- Warn: Project is vulnerable to: GHSA-vx3p-948g-6vhq
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
- Warn: Project is vulnerable to: GHSA-p9pc-299p-vxgp
Score
2.4
/10
Last Scanned on 2025-03-10
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to inversify-react
inversify-props
Wrapper to use inversify as property decorator with TypeScript, useful for component libraries like Vue, React or LitElement
@ablestack/inversify-react
A copy of Kukkimonsuta/inversify-react - Components and decorators to connect react with inversify - only intended for ablestack consumers
inversify-react-native
Components and decorators to connect react with inversify.
react-inversify
Dependency injection for React