inversify-hooks
TypeScript icon, indicating that this package has built-in type declarations

2.1.6 • Public • Published

Inversify Hooks

This package is a wrapper of inversify-props to simplify how inject your dependencies in components with hooks.

GitHub last commit GitHub license GitHub forks GitHub contributors GitHub issues

logo

Installation

$ npm install inversify-hooks reflect-metadata --save

The inversify-hooks type definitions are included in the inversify-hooks npm package.

How to use

import 'reflect-metadata'; // Import only once
import { container, useInject } from 'inversify-hooks';

container.addSingleton<IService1>(Service1);

function ExampleComponent() {
  const [service1] = useInject<IService1>(cid.IService1);

  useEffect(() => {
    if (!service1) {
      return;
    }

    service1.asyncMethod();
  }, [])
}

You can also use any ID that you prefer

container.addSingleton<IService1>(Service1, 'MyService1');

function ExampleComponent() {
  const service1 = useInject<IService1>('MyService1');
}

⚠️ Important! inversify-hooks requires TypeScript >= 2.0 and the experimentalDecorators, emitDecoratorMetadata, types and lib compilation options in your tsconfig.json file.

{
    "compilerOptions": {
        "target": "es5",
        "lib": ["es6"],
        "types": ["reflect-metadata"],
        "module": "commonjs",
        "moduleResolution": "node",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true
    }
}

How to test

There are more examples of how to test in inversify-props

More examples

There are more examples of how to use the container in services or other components in inversify-props

Why we made this package

You can learn more about why we made this packages in the original repo.

How register a dependency

If you're not familizared of how to register dependencies, check the docs.

How to configure Uglify or Terser

f you're using Uglify or Terser you need to configure well the plugin, check the docs.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
2.1.61,014latest
2.0.0-alpha20alpha

Version History

VersionDownloads (Last 7 Days)Published
2.1.61,014
2.1.50
2.1.40
2.1.20
2.1.10
2.1.00
2.0.00
2.0.0-alpha30
2.0.0-alpha20
2.0.0-alpha10
1.5.110
1.5.100
1.5.70
1.5.60
1.5.50
1.5.40
1.5.30
1.5.20
1.5.10
1.5.00
1.4.40
1.4.30
1.4.20
1.4.10
1.4.00
1.3.930
1.3.920
1.3.910
1.3.90
1.3.70
1.3.60
1.3.20
1.3.00
1.2.10
1.2.00
1.0.10
1.0.00

Package Sidebar

Install

npm i inversify-hooks

Weekly Downloads

1,014

Version

2.1.6

License

MIT

Unpacked Size

9.79 kB

Total Files

14

Last publish

Collaborators

  • ckgrafico