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

@brushy/di-react

v2.0.0

Published

React and React Native bindings for @brushy/di-core

Downloads

121

Readme

@brushy/di-react

React and React Native bindings for @brushy/di-core. Part of the @brushy/di 2.0 umbrella.

Installation

npm install @brushy/di-react @brushy/di-core
# or
yarn add @brushy/di-react @brushy/di-core
# or
pnpm add @brushy/di-react @brushy/di-core

Peer dependencies:

  • react >= 18.0.0
  • @brushy/di-core (runtime dependency of this package; install explicitly when not using @brushy/di)

Works in web and React Native projects. The react-native field in package.json points to the ESM build so Metro picks the correct bundle automatically.

Web usage

Create a stable Container outside the render tree and pass it to BrushyDIProvider:

import { Container, createToken } from '@brushy/di-core';
import { BrushyDIProvider, useInject, useInjectLazy } from '@brushy/di-react';

const container = new Container();
const USER_SERVICE = container.register(createToken('USER_SERVICE'), {
  useClass: UserService,
  lifecycle: 'singleton',
});

export function AppProviders({ children }: { children: React.ReactNode }) {
  return (
    <BrushyDIProvider container={container}>
      {children}
    </BrushyDIProvider>
  );
}

function Profile() {
  const userService = useInject(USER_SERVICE); // typed, no generic needed
  const reportService = useInjectLazy(REPORT_SERVICE); // resolved on first access
  // ...
}

For large apps, prefer the Context-first pattern: keep the container stable (created once at module level or memoized in the app) and avoid relying on the global registry inside render.

Component injection

Register components on the container with createToken (runtime Symbol, no collisions). Types infer from useValue:

import { Container, createToken } from '@brushy/di-core';
import { BrushyDIProvider, useInjectComponent } from '@brushy/di-react';

const container = new Container();

const DASHBOARD = container.register(createToken('DASHBOARD'), {
  useValue: DashboardPage,
});

function App() {
  const Dashboard = useInjectComponent(DASHBOARD);

  return (
    <BrushyDIProvider container={container}>
      <Dashboard />
    </BrushyDIProvider>
  );
}

Do not use string tokens ("DASHBOARD"). Prefer createToken('DASHBOARD') or Symbol('DASHBOARD').

React Native usage

The same provider and hooks work in React Native.

import { Text, View } from 'react-native';
import { Container, createToken } from '@brushy/di-core';
import { BrushyDIProvider, useInject } from '@brushy/di-react';

const container = new Container();
const API_URL = container.register(createToken('API_URL'), {
  useValue: 'https://api.example.com',
});

export function AppProviders({ children }: { children: React.ReactNode }) {
  return <BrushyDIProvider container={container}>{children}</BrushyDIProvider>;
}

function HomeScreen() {
  const apiUrl = useInject(API_URL);
  return (
    <View>
      <Text>API: {apiUrl}</Text>
    </View>
  );
}

React Native: register native components via tokens. For dev error UI from useInjectComponent, set a platform renderer once at app bootstrap:

import { Text, View } from 'react-native';
import { setInjectComponentErrorRenderer } from '@brushy/di-react';

setInjectComponentErrorRenderer((message, details) => (
  <View>
    <Text>{message}</Text>
    {details ? <Text>{details}</Text> : null}
  </View>
));

Without a custom renderer, non-DOM environments return null (errors still log to console.error).

Server usage

React hooks must not run during server rendering. Use @brushy/di-core on the server:

import { Container, runInRequestScope } from '@brushy/di-core';

const container = new Container();

app.get('/api/users', async (req, res) => {
  const result = await runInRequestScope(() => container.resolve(USER_SERVICE).list());
  res.json(result);
});

For integrating with React server components or non-React code, use the explicit bridge API (registerReactContainer / unregisterReactContainer) outside the render lifecycle.

Public API

| Export | Description | | --- | --- | | BrushyDIProvider | Provides the DI container via React context | | useInject | Eager dependency injection with optional promise caching | | useInjectLazy | Lazy proxy injection (resolve on first access) | | useInjectComponent | Resolve React components registered in the container | | registerComponent | Register a single component token | | registerComponents | Register a map of component tokens | | createComponentsProvider | Helper provider that registers components once | | setInjectComponentErrorRenderer | Custom dev error UI (required for RN) | | useDIContainer | Access the container from context | | registerReactContainer / unregisterReactContainer | Bridge for server / legacy integration | | bridgeContainer | Attach a container to the React registry | | ROOT_SCOPE, DIContext | Scope and context primitives |

Types: BrushyDIProviderProps, InjectComponentErrorRenderer, InjectOptions.

Large apps

  • Keep the container stable and pass it directly to BrushyDIProvider.
  • Do not trigger registry mutations or module registration during render.
  • Use the bridge API only when integrating with legacy code, server middleware, or non-React modules.
  • Register components with container.register(createToken('…'), { useValue }) during bootstrap, not inside components.

Related

| Package | Role | | --- | --- | | @brushy/di | Umbrella install | | @brushy/di-core | Container, tokens, request scope |