Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
inversify-react
Advanced tools
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
import { Provider } from 'inversify-react';
...
<Provider container={myContainer}>
...
</Provider>
Use dependencies from that container in child components
import { resolve, useInjection } from 'inversify-react';
...
// In functional component – via hooks
const ChildComponent: React.FC = () => {
const foo = useInjection(Foo);
...
};
// or in class component – via decorated fields
class ChildComponent extends React.Component {
@resolve
private readonly foo: Foo;
...
}
<Provider container={myContainer}>
...
</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.import * as React from 'react';
import { Container } from 'inversify';
import { Provider } from 'inversify-react';
// in functional component
const AppOrModuleRoot: React.FC = () => {
return (
<Provider container={() => {
const container = new Container();
container.bind(Foo).toSelf();
container.bind(Bar).toSelf();
return container;
}}>
{/*...children...*/}
</Provider>
);
};
// or class component
class AppOrModuleRoot extends React.Component {
// you can create and store container instance explicitly,
// or use factory function like in functional component example above
private readonly container = new Container();
constructor(props: {}, context: {}) {
super(props, context);
const { container } = this;
container.bind(Foo).toSelf();
container.bind(Bar).toSelf();
}
render() {
return (
<Provider container={this.container}>
{/*...children...*/}
</Provider>
);
}
}
const foo = useInjection(Foo);
// e.g. Foo and Bar are not bound
const foo = useOptionalInjection(Foo); // will return undefined
// or
const bar = useOptionalInjection(Bar, () => 'defaultBar'); // will return 'defaultBar'
const foo = useOptionalInjection(Foo, () => myDefault);
// foo === myDefault
// ^ Foo | typeof myDefault
That function conveniently receives container as argument, so you could instantiate your default using container (e.g. if it has dependencies)
const foo = useOptionalInjection(Foo, container => container.resolve(X));
const container = useContainer();
// or
const foo = useContainer(container => container.resolve(Foo));
const bars = useAllInjections(Bar);
For more examples, please refer to tests: test/hooks.tsx
@resolve
foo: Foo;
// or strict and semantic, see tips below
@resolve
private readonly foo!: Foo;
reflect-metadata
and emitDecoratorMetadata
// or pass service identifier explicitly
// e.g. if you deal with interfaces and/or don't want to use field type (via reflect-metadata)
@resolve(IFooServiceId)
private readonly foo!: IFoo;
@resolve.optional
private readonly foo?: Foo;
undefined
if service cannot be obtainedreflect-metadata
and emitDecoratorMetadata
@resolve.optional(serviceId, defaultValue?)
defaultValue
if service cannot be obtainedclass ChildComponent extends React.Component {
@resolve
private readonly foo!: Foo;
@resolve(Bar)
private readonly bar!: Bar;
@resolve.optional(Baz)
private readonly opt?: Baz;
...
}
// you can also use dependency in constructor,
// just don't forget to call super with context
// @see https://github.com/facebook/react/issues/13944
constructor(props: {}, context: {}) {
super(props, context);
console.log(this.foo.name);
}
@resolve.all('Foo')
private readonly foo!: Foo[];
reflect-metadata
and emitDecoratorMetadata
, but cannot be used without explicitly specifying service identifier@resolve.all(serviceId)
class ChildComponent extends React.Component {
@resolve.all(Baz)
private readonly all!: Baz[];
...
}
@resolve.optional.all('Foo')
private readonly foo!: Foo[];
reflect-metadata
and emitDecoratorMetadata
, but cannot be used without explicitly specifying service identifier@resolve.optional.all(serviceId)
class ChildComponent extends React.Component {
@resolve.optional.all(Baz)
private readonly allorempty!: Baz[];
...
}
[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.
export interface IFoo {
// ...
}
export namespace IFoo {
export const $: interfaces.ServiceIdentifier<IFoo> = Symbol('IFoo');
}
container.bind(IFoo.$).to(...);
// ^ no need to specify generic type,
// type gets inferred from explicit service identifier
// in constructor injections (not in React Components, but in services/stores/etc)
constructor(@inject(IFoo.$) foo: IFoo)
// in React Class component
@resolve(IFoo.$)
private readonly foo!: IFoo; // less imports and less chance of mix-up
// in functional component
const foo = useInjection(IFoo.$); // inferred as IFoo
FAQs
Components and decorators to connect react with inversify.
The npm package inversify-react receives a total of 12,448 weekly downloads. As such, inversify-react popularity was classified as popular.
We found that inversify-react demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.