effex
v4.0.2
Published
Redux side effects, inspired by Elm
Downloads
5
Maintainers
Readme
effex
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;
}
}