react-redux-local



The problem
I love redux, but building a small and simple local reducer component on every project is not on top of the list of things I like to do the most, plus what if I want to take advantage of sagas, dev tools and the new context api? It becomes a not so simple component very quickly.
The solution
You can think of react-redux-local
as a mini, yet powerful version of react-redux, the api is very simple, abstracting away things like creating a redux store, adding middleware, binding actions and plugging in the redux dev tools.
Table of Contents
Installation
This module is distributed via npm which is bundled with node and
should be installed as one of your project's dependencies
:
yarn add react-redux-local
Usage
LocalReducer
import LocalReducer from 'react-redux-local'
import { actions, reducer, saga, middleware, devToolsOptions } from './duck'
const App = () => (
<LocalReducer
actions={actions}
reducer={reducer}
saga={saga}
middleware={middleware}
devToolsOptions={devToolsOptions}
>
{(state, actions, dispatch) => (
<YourComponent state={state} actions={actions} />
)}
</LocalReducer>
)
createContext
import { createContext } from 'react-redux-local'
import { actions, reducer, saga, middleware, devToolsOptions } from './redux'
const { Provider, Consumer } = createContext({
actions,
reducer,
saga,
middleware,
devToolsOptions,
})
const Up = () => (
<Consumer mapActions={({ countUp }) => countUp}>
{(_, action) => <button onClick={action}>UP</button>}
</Consumer>
)
const Down = () => (
<Consumer mapActions={({ countDown }) => countDown}>
{(_, action) => <button onClick={action}>DOWN</button>}
</Consumer>
)
const Count = () => (
<Consumer mapState={({ counter }) => counter}>
{state => <h3>Count: {state}</h3>}
</Consumer>
)
const TotalCount = () => (
<Consumer mapState={({ total }) => total}>
{state => <h3>Total count: {state}</h3>}
</Consumer>
)
const DownsOnly = () => (
<Consumer mapState={({ downs }) => downs}>
{state => <h3>Downs: {state}</h3>}
</Consumer>
)
const App = () => (
<Provider>
<Up />
<Down />
<Count />
<TotalCount />
<DownOnly />
</Provider>
)
Api
Props
Tip: createContext
takes the same props as LocalReducer
reducer
func.isRequired
A reducer specifies how the application's state changes in response to actions sent to the store.
Learn More
e.g.
const initialState = { counter: 0, total: 0, downs: 0 }
const reducer = (state = initialState, action) => {
switch (action.type) {
case 'COUNT_UP':
return {
counter: state.counter + 1,
total: state.total + 1,
downs: state.downs,
}
case 'COUNT_DOWN':
return {
counter: state.counter - 1,
total: state.total + 1,
downs: state.downs + 1,
}
default:
return state
}
}
actions
objectOf(func.isRequired).isRequired
Actions are payloads of information that send data from your application to your store. They are the only source of information for the store.
Learn More
e.g.
const actions = {
countUp: () => ({ type: 'COUNT_UP' }),
countDown: () => ({ type: 'COUNT_DOWN' }),
}
saga
func
Aims to make application side effects (i.e. asynchronous things like data fetching and impure things like accessing the browser cache) easier to manage, more efficient to execute, simple to test, and better at handling failures.
Learn More
e.g.
import { put } from 'redux-saga'
function* doubleCount() {
put(actions.countUp())
}
function* saga() {
yield takeEvery('COUNT_UP', doubleCount)
}
middleware
arrayOf(func.isRequired)
It provides a third-party extension point between dispatching an action, and the moment it reaches the reducer.
Learn More
const middleware = store => next => action => {
console.log(action.type)
return next(action)
}
devToolsOptions
object
Allows for a better development experience with redux.
Dev Tools
Learn More
e.g.
const devToolsOptions = { name: 'My own devtools tab' }
children
func.isRequired
The term “render prop” refers to a simple technique for sharing code between React components using a prop whose value is a function.
Learn More
Video: Michael Jackson - Never Write Another HoC

<Consumer />
props (from createContext
)
mapState
func | state => undefined
Behaves like mapStateToProps
from react-redux
with the exception that it won't be available in the props (duh) and you are not required to return an object (thank you render props)
mapActions
func | (actions, dispatch) => undefined
Allows you to pick what actions you want available in the second argument of your render function. dispatch
is very much optional since all the actions are binded automatically.
<LocalReducer />
render function
;(state, actions, dispatch) => <YourComponent />
state
Your application state.
actions
Binded actions. (You don't need to dispatch)
dispatch
Optional function that allows you to dispatch other actions.
dispatch({ type: 'VERY_CUSTOM_ACTION' })
Examples
Other Solutions
local-react-redux
local-react-redux-saga
react-local-reducer
react-copy-write
LICENSE
MIT