0.3.2 • Published 9 years ago

petiole-thunk v0.3.2

Weekly downloads
1
License
MIT
Repository
github
Last release
9 years ago

petiole-thunk

Thunk plugin for Petiole.

Example of leaf using thunk

// users.js
import { declareLeaf } from './petiole';

export default const users = declareLeaf({
    initialState: {
        names: [],
        isLoading: false,
        error: false,
    },
    actions: {
        // My thunk:
        fetch: () => dispatch => {
            dispatch(); // dispatches { type: 'users/fetch' }
            ClientAPI.fetchUsers()
                .then(this.receive)
                .catch(this.fetchError);
        },
        receive: 'names', // shortcut for (names) => ({ type: 'users/names', names })
        fetchError: true, // shortcut for () => ({ type: 'users/fetchError' })
    },
    selectors: {
        userCount: state => state.names.length,
    },
    reducer: {
        fetch: state => state.set('isLoading', true),
        receive: (state, { names }) => state.merge({ names, isLoading: false }),
        fetchError: state => state.set('error', true),
    },
})
0.3.2

9 years ago

0.3.0

9 years ago

0.2.4

9 years ago

0.2.3

9 years ago

0.2.2

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago