Giter Club home page Giter Club logo

react-provided's Introduction

react-provided

A dead simple dependency injection library for React inspired by Recoil.

Usage

image showing layers of providers grouping different dependencies

First create a "provider layer" by using the Provider component.

The Provider component has a setup function with a couple of useful methods:

  • get - get a dependency from the parent layer
  • getOr - get a dependency from the parent layer, or fun a function if it doesn't exist
  • provide - provide a dependency on the current layer, everything will have access to this down the tree

Creating a layer looks like so:

<Provider setup={({ get, provide }) => {

  const myDep = get(MyDep);
  const myClass = get(MyClass);

  provide(MyOtherClass, () => new MyOtherClass(myClass, myDep));

}}>
  ...
</Provider>

Now that we've created a layer and provided the MyOtherClass dependency, it can be accessed from child components using the useDep hook.

It'll look something like this:

function ChildComponent() {
  const myOtherClass = useDep(MyOtherClass);

  // Do stuff with myOtherClass - it'll always be a stable reference.
}

Cleanup

When a Provider layer is destroyed, you have the option to run some cleanup methods. To do this, all you have to do is have a destroy method on your class. It will be called when the layer is going to be destroyed.

Why use this library?

  • You don't want to be 100% married to React
  • You're sick of your references not being stable
  • You don't want to create hundreds of hooks
  • React suggests not using useMemo for instantiating classes
  • You want to share data around without having to worry about re-rendering components

react-provided's People

Contributors

bennetthardwick avatar

Stargazers

GAURAV avatar Timo Hillmann avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.