Miss any of our Open RFC calls?Watch the recordings here! »

effex

4.0.2 • Public • Published

effex

npm package downloads dependencies devDependencies

This package allows you to easily describe side effects in your reducer, rather than middleware. This allows you to pass local reducer state to your side effects.

import { createStore, compose, applyMiddleware } from "redux";
import { combineReducers, createStoreWithEffects } from "effex";
import * as reducers from "./reducers";
import middleware from "./middleware";
 
// create side-effect aware reducer
const reducer = combineReducers(reducers);
 
// create side-effect aware store
const enhancer = compose(
  applyMiddleware(middleware),
  createStoreWithEffects()
);
 
const store = createStore(reducer, preloadedState, enhancer);
import { combineReducers, createStateWithEffects } from "effex";
import { USER_LOGGED_IN, USER_TOKEN_STORED, userTokenStored } from "./actions";
 
// side effects perform async actions and may return an action to be dispatched
async function storeUserToken(token) {
  await SecureStorage.setItem("USER_TOKEN", token);
 
  return userTokenStored(token);
}
 
// side effects can use other side effects
async function clearUserToken() {
  return storeUserToken(null);
}
 
// declare your side effects in your reducer
// this gives you access to the action payload and local state
export default function tokenReducer(state = null, action) {
  switch (action.type) {
    case USER_LOGGED_IN:
      return createStateWithEffects(state, () => storeUserToken(action.payload.token));
 
    case USER_LOGGED_OUT:
      return createStateWithEffects(state, clearUserToken);
 
    case USER_TOKEN_STORED:
      return action.payload;
 
    default:
      return state;
  }
}

Install

npm i effex

DownloadsWeekly Downloads

1

Version

4.0.2

License

MIT

Unpacked Size

5.26 kB

Total Files

6

Last publish

Collaborators

  • avatar