npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

injection-react

v0.0.5

Published

inject with react. 在 react 中使用依赖注入帮助项目代码接耦合,快速迭代,快速复用。

Readme

injection-react

Build Status Downloads

一个可以在react项目中使用的JavaScript和TypeScript依赖注入库。它是Angular依赖注入的一部分,这意味着它具有完整,快速,可靠且经过良好测试的功能。

如何不使用Angular获得依赖注入的能力?

Angular 5版本弃用了ReflectiveInjector API,并引入了StaticInjector。 简而言之,最新版本的Angular中的依赖项注入将完全在编译时进行,因此不需要进行反射。

但是,如果您想在Node.js,Vue,React,Vanilla JS,TypeScript等应用程序中使用依赖项注入,您将无法像Angular那样利用“ StaticInjector”优势,因为您的应用程序无法与Angular编译器兼容。

这意味着如果您的react项目需要在Angular@angular/core之外进行依赖注入是不可以的。在这种情况下,请使用injection-react以获得快速,小型,可靠,高质量,设计良好且经过测试的解决方案。

How to use?

$ npm i injection-react injection-js
# OR
$ yarn add injection-react injection-js

Note:

对于ES6Class语法和TypeScript,您需要Reflect API的polyfill. You can use:

同样对于TypeScript,您将需要在您的tsconfig.json中设置experimentalDecoratorsemitDecoratorMetadatatrue

TypeScript

import 'reflect-metadata';
import React from 'react';
import { Injectable } from 'injection-js';
import { render, Component } from 'injection-react';

@Injectable()
class Service {
  getHello(): string {
    return 'Hello World!';
  }
}

class App extends Component {
    render() {
        return (
            <div>{this.get('Service').getHello()}</div>
        );
    }
}

render([{ provide: 'Service', useClass: Service }], <App />, document.body);

or

import 'reflect-metadata';
import React from 'react';
import { Injectable, Injector } from 'injection-js';
import { render, Component, stateLessComponentWithInjector } from 'injection-react';

@Injectable()
class Service {
  getHello(): string {
    return 'Hello World!';
  }
}

const App = stateLessComponentWithInjector((_, injector: Injector) => (
  <div>{injector.get('Service').getHello()}</div>
));

render([{ provide: 'Service', useClass: Service }], <App />, document.body);

API

有关完整的文档,请查看Angular DI文档:

License

BSD 3-Clause