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 " ;
const reducer = combineReducers ( reducers ) ;
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 " ;
async function storeUserToken ( token ) {
await SecureStorage . setItem ( " USER_TOKEN " , token ) ;
return userTokenStored ( token ) ;
}
async function clearUserToken ( ) {
return storeUserToken ( null ) ;
}
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 ;
}
}