Are you happy with your logging solution? Would you help us out by taking a 30-second survey? Click here

react-simple-di

Simple Dependancy Injection Solution for React

Subscribe to updates I use react-simple-di


Statistics on react-simple-di

Number of watchers on Github 52
Number of open issues 10
Average time to close an issue about 2 months
Main language JavaScript
Average time to merge a PR about 4 hours
Open pull requests 5+
Closed pull requests 0+
Last commit almost 3 years ago
Repo Created over 3 years ago
Repo Last Updated over 1 year ago
Size 16 KB
Organization / Authorkadirahq
Contributors2
Page Updated
Do you use react-simple-di? Leave a review!
View open issues (10)
View react-simple-di activity
View on github
Fresh, new opensource launches 🚀🚀🚀
Trendy new open source projects in your inbox! View examples

Subscribe to our mailing list

Evaluating react-simple-di for your project? Score Explanation
Commits Score (?)
Issues & PR Score (?)

react-simple-di

Simple dependancy injection solution for React.

Installation

npm i react-simple-di

Intro

In react-simple-di, we've two types of dependencies, they are:

  1. context - These are usually, configurations, models and client for different remote data solutions.
  2. actions - Actions are simple functions which used to perform business logic with the help of the above context.

Every action will receive the context as it's first argument.

Injecting Dependancies

First, we need to inject dependencies to a root level React component. Mostly, this will be the main layout component of our app.

Here are our dependencies:

const context = {
    DB,
    Router,
    appName: 'My Blog'
};

const actions = {
    posts: {
        create({DB, Router}, title, content) {
            const id = String(Math.random());
            DB.createPost(id, title, content);
            Router.go(`/post/${id}`);
        }
    }
};

First we've defined our context. Then, we have our actions. Here actions must follow a structure like mentioned above.

Let's inject our dependencies:

import {injectDeps} from 'react-simple-di';
import Layout from './layout.jsx';

// Above mentioned actions and context are defined here.

const LayoutWithDeps = injectDeps(context, actions)(Layout);

Now you can use LayoutWithDeps anywhere in your app.

Using Depedencies

Any component rendered inside LayoutWithDeps can access both context and actions.

When using dependecies it will compose a new React component and pass dependencies via props to the original component.

First let's create our UI component. Here it will expect dependecies to come via props appName and createPost.

class CreatePost extends React.Component {
    render() {
        const {appName} = this.props;
        return (
            <div>
                Create a blog post on app: ${appName}. <br/>
                <button onClick={this.create.bind(this)}>Create Now</button>
            </div>
        );
    }

    create() {
        const {createPost} = this.props;
        createPost('My Blog Title', 'Some Content');
    }
}

So, let's use dependencies:

import {useDeps} from 'react-simple-di';

// Assume above mentioned CreatePost react component is
// defined here.

const depsToPropsMapper = (context, actions) => ({
    appName: context.appName,
    createPost: actions.posts.create
});

const CreatePostWithDeps = useDeps(depsToPropsMapper)(CreatePost);

That's it.

Note: Here when calling the actions.posts.create action, you don't need to provide the context as the first argument. It'll handle by react-simple-di.

Default Mapper

If you didn't provide a mapper function, useDeps will use a default mapper function will allows you to get context and props directy. Here's that default mapper:

const mapper = (context, actions) => ({
    context: () => context,
    actions: () => actions
});
react-simple-di open issues Ask a question     (View All Issues)
  • about 3 years pass props to mapper
  • over 3 years Support nested actions
  • over 3 years Dependancy Injection error when multiple call injectDeps()
react-simple-di open pull requests (View All Pulls)
  • actions get actions injected as second arg
  • Ability to module-namespace actions
  • Fix React 15.5 warnings.
  • Support for React version 16.0.0
  • Add React 16 to peerDependencies
react-simple-di list of languages used
Other projects in JavaScript