Coder Social home page Coder Social logo

ptzagk / redux-injectors Goto Github PK

View Code? Open in Web Editor NEW

This project forked from react-boilerplate/redux-injectors

0.0 0.0 0.0 362 KB

Asynchronous injectors for Redux reducers and sagas. As used by react-boilerplate.

License: MIT License

JavaScript 98.87% Shell 1.13%

redux-injectors's Introduction

alt text

build status

Dynamically load redux reducers and redux-saga sagas as needed, instead of loading them all upfront. This has some nice benefits, such as avoiding having to manage a big global list of reducers and sagas. It also allows more effective use of code-splitting. See motivation. As used by react-boilerplate.

Getting Started

npm install redux-injectors # (or yarn add redux-injectors)

Setting up the redux store

The redux store needs to be configured to allow this library to work. The library exports a store enhancer that can be passed to the createStore function.

import { createStore } from "redux";
import { createInjectorsEnhancer } from "redux-injectors";

const store = createStore(
 createReducer(),
 initialState,
 createInjectorsEnhancer({
   createReducer,
   runSaga,
 })
)

Note the createInjectorsEnhancer function takes two options. createReducer should be a function that when called will return the root reducer. It's passed the injected reducers as an object of key-reducer pairs.

function createReducer(injectedReducers = {}) {
 const rootReducer = combineReducers({
   ...injectedReducers,
   // other non-injected reducers can go here...
 });

 return rootReducer
}

runSaga should usually be sagaMiddleware.run.

  const runSaga = sagaMiddleware.run;

Injecting your first reducer and saga

After setting up the store, you will be able to start injecting reducers and sagas.

import { compose } from "redux";
import { injectReducer, injectSaga } from "redux-injectors";

class BooksManager extends React.PureComponent {
 render() {
   return null;
 }
}

export default compose(
  injectReducer({ key: "books", reducer: booksReducer }),
  injectSaga({ key: "books", saga: booksSaga })
)(BooksManager);

Or, using hooks:

import { useInjectReducer, useInjectSaga } from "redux-injectors";

export default function BooksManager() {
  useInjectReducer({ key: "books", reducer: booksReducer });
  useInjectSaga({ key: "books", saga: booksSaga });

  return null;
}

Documentation

See the API reference

Motivation

There's a few reasons why you might not want to load all your reducers and sagas upfront:

  1. You don't need all the reducers and sagas for every page. This library lets you only load the reducers/sagas that are needed for the page being viewed. This speeds up the page load time because you can take advantage of code-splitting. This is also good for performance after the page has loaded, because fewer reducers and sagas are running.
  2. You don't want to have to manage a big list of reducers/sagas. This library lets components inject their own reducers/sagas, so you don't need to worry about adding reducers/sagas to a global list.

redux-injectors's People

Contributors

benlorantfy avatar julienben avatar frozenkiwi 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.