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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@injex/react-plugin

v4.0.0-alpha.2

Published

<img src="https://img.shields.io/npm/v/@injex/react-plugin?style=for-the-badge" />

Downloads

35

Readme

React Plugin

The React plugin makes it easier to inject dependencies from an Injex container into React components using react hooks. The Plugin creates a Context provider and exposes the useInjex() hook, so you can use Injex container API to inject your application modules into your application components.

Installation

You can install the React Plugin via NPM

npm install --save @injex/react-plugin

or Yarn

yarn add @injex/react-plugin

You should also make sure React and ReactDOM is installed on your project.

Initialization

Creating the plugin and passing it to the runtime container config object

import { Injex } from "@injex/node";
import { ReactPlugin } from "@injex/react-plugin";

Injex.create({
    rootDirs: [__dirname],
    plugins: [
        new ReactPlugin({
            // plugin configurations
        })
    ]
});

Configurations

render

The render function from React. The default is the render function from react-dom. This configuration is pretty rare and it used where you have more than one react version in your application.

  • Type: function
  • Default: render from react-dom
  • Required: false

rootElementOrSelector

An HTML element or string selector to use with the RenderInjexProvider method.

  • Type: HTMLElement | string
  • Default: null
  • Required: false

Usage

The Injex React plugin is slightly different from the other plugins in a way you can use it without creating a plugin instance; here is a basic usage example.

import { Injex } from "@injex/webpack";
import { ReactPlugin } from "@injex/react-plugin";

Injex.create({
    resolveContext: () => require.context(__dirname, true, /\.tsx?$/),
    plugins: [
        new ReactPlugin({
            rootElementOrSelector: "#root"
        })
    ]
}).bootstrap();

The rootElementOrSelector option tells the plugin where is the root container element for rendering the application, this is not a mandatory configuration and it's relevant only if you're going to use the renderInjexProvider method as described below.

The renderInjexProvider method

The most straightforward and easy way to use the plugin is by rendering your application using this injectable method. It will render your root component inside an Injex provider so you can use the useInjex() hook anywhere in your React application components.

import * as React from "react";
import { bootstrap, IBootstrap, inject } from "@injex/core";
import { RenderInjexProvider } from "@injex/react-plugin";
import App from "components/app";

@bootstrap()
export class Bootstrap implements IBootstrap {

    @inject() private renderInjexProvider: RenderInjexProvider;

    public run() {
        this.renderInjexProvider(<App />);
    }
}

The renderInjexProvider injectable method accepts two arguments. The first is the Root component we want to render into the container provided in the rootElementOrSelector plugin option. The second argument is optional, and it accepts the root element for rendering the component in case the rootElementOrSelector option was not provided; this will allow using the method multiple times with different root elements.

The method will render your root component inside an InjexProvider component to enable the use of the useInjex() hook.

Manually rendering the InjexProvider

Sometimes you'll want to render the InjexProvider by yourself. Injex React plugin exposes the InjexProvider so you can use it while rendering your application. The provider accepts only one prop, the Injex runtime container itself, and you can access it using the @inject() decorator.

import * as React from "react";
import { render } from "react-dom";
import { bootstrap, IBootstrap, inject } from "@injex/core";
import { InjexProvider } from "@injex/react-plugin";
import App from "components/app";

@bootstrap()
export class Bootstrap implements IBootstrap {

    @inject() private $injex;

    public run() {
        render(
            <InjexProvider container={this.$injex}>
                <App />
            </InjexProvider>,
            document.getElementById("root")
        );
    }
}

Using the useInjex() hook

The useInjex() hook is the core of the Injex React plugin, making it possible to inject dependencies from your runtime container directly into your application components inside the InjexProvider.

Lets say you have a singleton session manager with a currentUser as a property:

import { define, singleton } from "@injex/core";

@define()
@singleton()
export class SessionManager {
    public get currentUser() {
        return {
            name: "Udi Talias",
            url: "https://twitter.com/uditalias"
        };
    }
}

You can inject it into your application components using the useInjex() hook:

import * as React from "react";
import { useInjex } from "@injex/react-plugin";

export default function App() {

    const [inject, injectAlias] = useInjex();

    // inject the singleton instance of the SessionManager
    const session = inject("sessionManager");

    return (
        <h1>
            Hello, <a href={session.currentUser.url}>{session.currentUser.name}</a>
        </h1>
    );
}

Note that useInjex() exposes two functions inside an array. The first is inject, which works the same as the @inject() decorator, and the second is the injectAlias that works the same as the @injectAlias() decorator.

If you want a quick demo to play with, check out the react example in the examples section.