This package provides utilites for working with Redux within a Mithril application. It works almost exactly like react-redux
.
It subscribes to the store and sets state.props
to values provided by two mapping functions, one that maps state
and the other that maps dispatch
.
It does not rely on you using the mjsx
Babel plugin.
This is a mostly complete rewrite of colinbate/mithril-redux, which was compatible with Mithril 0.2. The new version changes the API for connect()
(making it work like react-redux
) and also makes the library compatible with Mithril 1.0.
Install with:
npm install --save mithril-redux
When you are mounting your app, you should call this function to initialize the link between your store and Mithril.
The parameters are the Redux store, then the mithril (m
) object, and then an optional mithril component. If the component is a function is it instantiated and returned, otherwise it is returned as-is.
import m from 'mithril';
import {Provider} from 'mithril-redux';
import configStore from './store';
import Root from './root';
const store = configStore({name: 'World', age: 30});
m.mount(document.body, Provider.init(store, m, Root));
Used to turn your Mithril components into Redux-aware Mithril components. Works similar to connect
from react-redux
.
mapStateToProps
- A function which provides the result ofstore.getState()
and the component's ownstate.props
variable and returns a list of props to add to the component'sstate.props
. This function is generally used for connecting store values to component props.mapDispatchToProps
- A function which providesstore.dispatch
and the component's ownstate.props
variable and returns a list of props to add to the component'sstate.props
. This function is generally used to connect action creators to Redux'sdispatch
function.component
- The Mithril component to connect.
import {connect} from 'mithril-redux';
import {bindActionCreators} from 'redux'
import {incrementAge, decrementAge, resetAge} from './actions';
const AgeBox = {
view: function(vnode) {
const {state} = vnode;
return m('div', [
m('span', 'Age: ' + state.props.age),
m('button', {onclick: state.props.actions.dec}, 'Younger'),
m('button', {onclick: state.props.actions.inc}, 'Older'),
m('button', {onclick: state.props.actions.reset}, 'Reset')
]);
}
}
const mapStateToProps = state => ({
age: state.age
})
const mapDispatchToProps = dispatch => ({
actions: bindActionCreators({incrementAge, decrementAge, resetAge}, dispatch)
})
export default connect(mapStateToProps, mapDispatchToProps)(AgeBox);
connect
now accepts two functions as its first two parameters:- One that maps from
store.getState()
tostate.props
- One that maps from
store.dispatch
tostate.props
- One that maps from
- The component will now be subscribed to the redux store, rather than just get updates as part of the component's own lifecycle.