Gathering detailed insights and metrics for inversify-react
Gathering detailed insights and metrics for inversify-react
Gathering detailed insights and metrics for inversify-react
Gathering detailed insights and metrics for 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-di
Use DI in React in Angular-like way
inversify-react-native
Components and decorators to connect react with inversify.
Components and decorators to connect react with inversify.
npm install inversify-react
Typescript
Module System
Node Version
NPM Version
89.1
Supply Chain
97.8
Quality
76.9
Maintenance
100
Vulnerability
100
License
TypeScript (97.56%)
JavaScript (2.44%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
Apache-2.0 License
171 Stars
73 Commits
12 Forks
7 Watchers
2 Branches
4 Contributors
Updated on Apr 02, 2025
Minified
Minified + Gzipped
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
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Hooks and decorators for InversifyJS + React.
Table of Contents
TL;DR:
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:
inversify-react
also uses decorators, but only when used in Class Components.
Usage is pretty similar to React Context.
Wrap React component tree with Provider
and Container
from inversify-react
– just like React Context.Provider
1import { 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}
1<Provider container={myContainer}> 2 ... 3</Provider>
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}
1const foo = useInjection(Foo);
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'
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));
1const container = useContainer(); 2// or 3const foo = useContainer(container => container.resolve(Foo));
1const bars = useAllInjections(Bar);
For more examples, please refer to tests: test/hooks.tsx
1@resolve 2foo: Foo; 3 4// or strict and semantic, see tips below 5@resolve 6private readonly foo!: Foo;
reflect-metadata
and emitDecoratorMetadata
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;
1@resolve.optional 2private readonly foo?: Foo;
undefined
if service cannot be obtainedreflect-metadata
and emitDecoratorMetadata
@resolve.optional(serviceId, defaultValue?)
defaultValue
if service cannot be obtained1class 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}
1@resolve.all('Foo') 2private readonly foo!: Foo[];
reflect-metadata
and emitDecoratorMetadata
, but cannot be used without explicitly specifying service identifier@resolve.all(serviceId)
1class ChildComponent extends React.Component { 2 @resolve.all(Baz) 3 private readonly all!: Baz[]; 4 5 ... 6}
1@resolve.optional.all('Foo') 2private readonly foo!: Foo[];
reflect-metadata
and emitDecoratorMetadata
, but cannot be used without explicitly specifying service identifier@resolve.optional.all(serviceId)
1class ChildComponent extends React.Component { 2 @resolve.optional.all(Baz) 3 private readonly allorempty!: Baz[]; 4 5 ... 6}
[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
Reason
Found 3/28 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
55 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-30
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 More