0.0.2 • Published 7 years ago

hyperapp-deepupdate v0.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

Hyperapp DeepUpdate

Usage

install :

npm install --save hyperapp-deepupdate

A basic complete example

import { app, h } from 'hyperapp'
import { deepUpdateMixin } from 'hyperapp-deepupdate'

app({
    state  : {
        counters: [
            {
                value: 0,
            },
            {
                value: 2,
            },
        ],
    },
    view(state, actions) {
        return <div>
            <ul>
                {
                    state.counters.map((counter, i) => <div>
                        <p>{counter.value}</p>
                        <button type="button" onclick={() => actions.increment(i)}>Add</button>
                    </div>)
                }
            </ul>
        </div>
    },
    actions: {
        increment(state, actions, index) {
            return [ 'counters', index, 'value', state => state + 1 ]
        }
    },
    mixins : [ deepUpdateMixin ]
})

Using with thunk :

    actions: {
        increment(state, actions, index) {
            return update => update([ 'counters', index, 'value', state => state + 1 ])
        }
    },

Batching updates :

    actions: {
        increment(state, actions, index) {
            return [
              [ 'counters', index, 'value', state => state + 1 ],
              [ 'counters', index, 'value', state => state + 1 ],
          ]
        }
    },