react-redux-local
v3.4.0
Published
[![Build Status][build-badge]][build] [![Code Coverage][coverage-badge]][coverage] [![version][version-badge]][package] [![downloads][downloads-badge]][npmtrends]
Downloads
54
Maintainers
Readme
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'
// https://github.com/erikras/ducks-modular-redux
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>
)
// Will only rerender when the "counter" state changes
const Count = () => (
<Consumer mapState={({ counter }) => counter}>
{state => <h3>Count: {state}</h3>}
</Consumer>
)
// Will only rerender when the "total" state changes
const TotalCount = () => (
<Consumer mapState={({ total }) => total}>
{state => <h3>Total count: {state}</h3>}
</Consumer>
)
// Will only rerender when the "downs" state changes
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 asLocalReducer
reducer
func.isRequired
A reducer specifies how the application's state changes in response to actions sent to the store.
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.
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.
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.
const middleware = store => next => action => {
console.log(action.type)
return next(action)
}
devToolsOptions
object
Allows for a better development experience with redux.
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.
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
LICENSE
MIT