This sample illustrates how to use Marko with Redux. Redux is a general-purpose application state container. This sample app illustrates how easy it is to wire up a Marko UI component with a Redux store.
git clone https://github.com/marko-js-samples/marko-redux.git
cd marko-redux
npm install
npm start
The partial code snippet below shows how a a Marko UI component can be connected to a Redux store using the store.subscribe()
method and the Marko forceUpdate()
method:
import store from '../store';
class {
onMount() {
store.subscribe(() => {
// Force this UI component to rerender:
this.forceUpdate();
// The UI component will be rerendered using the new
// state returned by `store.getState()`
//
// The following is another option to force an update:
// this.input = store.getState();
});
}
}
<div>
<counter(store.getState()) ... />
</div>
In the above example, the imported store module exports a Redux store created using the following code:
var redux = require('redux');
var counter = require('./reducers');
module.exports = redux.createStore(counter);