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

react-bottle

v1.2.0

Published

A di container for react

Downloads

14

Readme

React-Bottle

CircleCI npm version

A React binding for BottleJS Dependency injection

Installation

Install via NPM: npm i react-bottle;

Quick Start

First you must instantiate a diContainer by using getProviderElement. getProviderElement takes in a series of functions that build up your dependencies and returns a Root DI provider element. Then you pass your components into wireUpDI to get your container injected into your components via the di prop.

Example

import {
    getProviderElement,
    wireUpDi,
    DIFragment,
} from "react-bottle";

let Component = (props) => {
    return (
        <div>
            <button onClick={props.di.onClick}>ClickMe</button>
        </div>
    )
}

Component = wireUpDi()(Component);

const buttonOnClickFragment:DIFragment = (di: ReactBottle): ReactBottle => {
    di.factory("onClick", () => () => alert("I was Clicked!"));
    return di;
}

const Provider = getProviderElement(buttonOnClickFragment);

ReactDOM.render(
    <Provider><Component /></Provider>,
    document.getElementById('root'),
);

Why even use a DI container

Because it makes testing easier. It separates your concerns and provides a simple method to change your dependencies when you either need to change your application behavior or when testing a specific unit. I found that DI containers forces you to think about being deliberate in what a component or function needs to do and what your dependencies of something are and needs to be.

On top of this, it makes testing easier by providing an easier mechanism for mocking your dependencies than just monkey patching.

Usage

getProviderElement

const getProviderElement = (...fragments: DIFragment[]): React.FunctionComponent<any>

Takes a list of Di Fragments (to help with DI organization) and returns the Root Provider element that should wrap your app.

DIFragment

type DIFragment = (container: ReactBottle) => ReactBottle;

A DIFragment is a function that takes a ReactBottle instance and returns a ReactBottle instance. These functions should be where you should define your dependencies. Define these functions to provide a logical separation in your DI container to provide better organization.

wireUpDi

(mapDiToProps?: DIMapper) => (Component: React.ComponentType<any>): React.ComponentType<any>

Injects your di container into your components. Takes an optional mapDiToProps function similar to react-redux's mapStateToProps: It takes the di container and then maps those to friendlier prop names.

useDI hook

const useDI = (override: any = {}) => any;

useDI is a react hook for your DI container. You can override or provide a DI container via the override for testing. The hook just returns your DI container.

ReactBottle

This is a react binding for BottleJS, a dead simple and powerful DI container. Anything you can do with that project you can do with this one. Docs for BottleJS: https://github.com/young-steveo/bottlejs