UNPKG

1.36 kBMarkdownView Raw
1# Redux + Marko
2
3See the [`marko-redux` sample project](https://github.com/marko-js-samples/marko-redux) for a fully-working example.
4
5## Installation
6
7First, save the [`marko`](https://www.npmjs.com/package/marko) and [`redux`](https://www.npmjs.com/package/redux) packages to your project’s dependencies:
8
9```bash
10npm i marko redux
11```
12
13## Usage
14
15The partial code below shows how a Marko UI component can connect to a Redux store, using Redux’s `store.subscribe()` method and Marko’s `forceUpdate()` method:
16
17### `counter.marko`
18
19```marko
20import store from './store';
21
22class {
23 onMount () {
24 store.subscribe(() => {
25 // Force this UI component to rerender
26 this.forceUpdate();
27
28 // The UI component will rerender with the new
29 // state returned by `store.getState()`
30 //
31 // You could also force an update like this:
32 // this.input = store.getState();
33 });
34 }
35}
36
37<counter(store.getState()) />
38```
39
40### `reducer.js`
41
42```js
43module.exports = function(state, action) {
44 state = state || { value: 0 };
45
46 // Additional reducer logic here…
47
48 return state;
49};
50```
51
52### `store.js`
53
54In `counter.marko`, the imported store module exports a Redux store created with the following code:
55
56```js
57const redux = require("redux");
58const counter = require("./reducer");
59
60module.exports = redux.createStore(counter);
61```