Skip to content

Latest commit

 

History

History
301 lines (196 loc) · 7.13 KB

api.md

File metadata and controls

301 lines (196 loc) · 7.13 KB

Table of Contents

Setup

createInjectorsEnhancer

Creates a store enhancer that when applied will setup the store to allow the injectors to work properly

Parameters

  • params Object
    • params.runSaga function A function that runs a saga. Should usually be sagaMiddleware.run
    • params.createReducer function A function that should create and return the root reducer. It's passed the injected reducers as the first parameter. These should be added to the root reducer using combineReducer or a similar method.

Examples

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

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

 return rootReducer
}
const runSaga = sagaMiddleware.run

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

Managers

createManager

Creates a "manager" component that will inject the provided reducer and saga when mounted. It only renders its children after both the reducer and saga have been injected. This is the recommended way to use redux-injectors.

Parameters

  • options Object
    • options.name string The name to give the manager that shows up in the react devtools
    • options.key string The key to inject the reducer under
    • options.reducer function? The reducer that will be injected
    • options.saga function? The saga that will be injected

Examples

const BooksManager = createManager({ name: "BooksManager", key: "books", reducer: booksReducer, saga: booksSaga })

Returns ComponentType<{children: ReactNode}> The manager

Injectors

injectReducer

A higher-order component that dynamically injects a reducer when the component is instantiated

Parameters

  • params Object
    • params.key string The key to inject the reducer under
    • params.reducer function The reducer that will be injected

Examples

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

export default injectReducer({ key: "books", reducer: booksReducer })(BooksManager)

useInjectReducer

A react hook that dynamically injects a reducer when the hook is run

Parameters

  • params Object
    • params.key string The key to inject the reducer under
    • params.reducer function The reducer that will be injected

Examples

function BooksManager() {
  useInjectReducer({ key: "books", reducer: booksReducer })

  return null;
}

Returns boolean flag indicating whether or not the reducer has finished injecting

injectSaga

A higher-order component that dynamically injects a saga when the component is instantiated. There are several possible "modes" / "behaviours" that dictate how and when the saga should be injected and ejected

Parameters

  • params Object
    • params.key string The key to inject the saga under
    • params.saga function The saga that will be injected
    • params.mode string? The injection behaviour to use. The default is SagaInjectionModes.DAEMON which causes the saga to be started on component instantiation and never canceled or started again. @see SagaInjectionModes for the other possible modes.

Examples

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

export default injectSaga({ key: "books", saga: booksSaga })(BooksManager)

useInjectSaga

A react hook that dynamically injects a saga when the hook is run

Parameters

  • params Object
    • params.key string The key to inject the saga under
    • params.saga function The saga that will be injected

Examples

function BooksManager() {
  useInjectSaga({ key: "books", saga: booksSaga })

  return null;
}

Returns boolean flag indicating whether or not the saga has finished injecting

Misc

forceReducerReload

Forces a reload of the injected reducers. i.e. Causes createReducer to be called again with the injected reducers. Useful for hot-reloading.

Parameters

  • store The redux store that has been configured with createInjectorsEnhancer

Examples

forceReducerReload(store);

SagaInjectionModes

An enum of all the possible saga injection behaviours

Properties

  • RESTART_ON_REMOUNT String The saga will be started on component instantiation and cancelled with task.cancel() on component unmount for improved performance.
  • DAEMON String Causes the saga to be started on component instantiation and never canceled or started again.
  • ONCE_TILL_UNMOUNT String Behaves like 'RESTART_ON_REMOUNT' but never runs it again.
  • COUNTER String Similar to 'RESTART_ON_REMOUNT' except the saga will be mounted only once on first inject and ejected when all injectors are unmounted. This enables you to have multiple injectors with the same saga and key and only one instance of the saga will run.