Simple wrapper around react-redux.
It allows to use different nested Provider
s by specifying a custom store
name.
It requires React 0.14 or later, and React Redux 4 or later (those being peerDependencies
).
npm install --save react-redux-custom-store
In normal cases you don't need this.
However, if your application starts growing and you want to decouple it, e.g. in different modules, you may need to have different Provider
s across the application.
This could also be applied for things like widgets, with their own store, actions, reducers, styles, etc. As such, the application will have different widgets but it doesn't know anything about them.
In oder words, I can have the main application just being the scaffolding with a global state (e.g. user
, language
, etc.) and having different parts of the UI as well as different views (e.g. mapped to specific routes) being modules, widgets or whatever.
Each module / widget will have then its own store (e.g. todos
, blog
, orders
, dashboard
, etc.) as well as actions, reducers and so on, and can still access the global state of the app.
This modules exposes two functions:
createProvider([storeName])
: given an optionalstoreName
(default beingstore
), it returns aProvider
component.
The implementation is the same as the one from
react-redux
, only with the custom store name used as thecontext
key.
connect(...)(Component, [storeName])
: the signature of this method is the same as the originalconnect
function ofreact-redux
. It accepts extra thestoreName
(default beingstore
), used to pick the related store from thecontext
and pass it down to the connected component as a prop.
// Note: you can still use the original exports of `react-redux`,
// as long as you use the default `store`.
// You can use the two libraries alongside, `react-redux-custom-store`
// uses `connect` from the `react-redux` library at the end.
// root.js
import { createStore, combineReducers } from 'redux'
import { Provider } from 'react-redux'
const store = createStore(combineReducers({
user: (/* state, action */) => ({ name: 'John' })
}))
// Uses the default store `store` as name.
const Root = () => (
<Provider store={store}>
<TodosContainer />
</Provider>
)
ReactDOM.render(<Root />, document.getElementById('app'))
// todos-container.js
import { createStore, combineReducers } from 'redux'
import { createProvider } from 'react-redux-custom-store'
const storeName = 'todo'
const todoStore = createStore(combineReducers({
todos: (/* state, action */) => [{ text: 'OK' }, { text: 'Missing' }],
}))
const Provider = createProvider(storeName)
// Uses the custom store `todo` as name.
// At this point there are 2 stores in `context`.
const TodosContainer = () => (
<Provider store={todoStore}>
<Todos />
</Provider>
)
// todos.js
import { connect } from 'react-redux-custom-store'
const TodoProfile = ({ name }) => (
<h2>{name}</h2>
)
TodoProfile.propTypes = {
name: PropTypes.string.isRequired
}
const TodoList = ({ todos }) => (
<ul>
{todos.map(({ text }) => (<li>{text}</li>))}
</ul>
)
TodoList.propTypes = {
todos: PropTypes.array.isRequired
}
const ConnectedTodoProfile = connect(
({ user: { name } }) => ({ name })
)(TodoProfile) // uses default store name `store`
const ConnectedTodoList = connect(
({ todos }) => ({ todos })
)(TodoList, 'todo') // uses custom store name `todo`
const Todos = () => (
<div>
<ConnectedTodoProfile />
<ConnectedTodoList />
</div>
)
MIT