1 | # puex
|
2 |
|
3 | [![NPM version](https://img.shields.io/npm/v/puex.svg?style=flat)](https://npmjs.com/package/puex) [![NPM downloads](https://img.shields.io/npm/dm/puex.svg?style=flat)](https://npmjs.com/package/puex) [![CircleCI](https://circleci.com/gh/egoist/puex/tree/master.svg?style=shield&circle-token=af0131758916e976003f5e909a703fe6821d3124)](https://circleci.com/gh/egoist/puex/tree/master) [![donate](https://img.shields.io/badge/$-donate-ff69b4.svg?maxAge=2592000&style=flat)](https://github.com/egoist/donate)
|
4 |
|
5 | ## Features
|
6 |
|
7 | - Small size, the goal is to have a 1kB state management library.
|
8 | - Similar to Vuex
|
9 | - Dropped unnecessary features (for specific use cases).
|
10 | - Every developer that knows Vuex knows Puex already.
|
11 | - `vue-devtools`: It just works™.
|
12 |
|
13 | ## Install
|
14 |
|
15 | ```bash
|
16 | yarn add puex
|
17 | ```
|
18 |
|
19 | CDN: [UNPKG](https://unpkg.com/puex/dist/) | [jsDelivr](https://cdn.jsdelivr.net/npm/puex/dist/)
|
20 |
|
21 | ## Usage
|
22 |
|
23 | ### Create store instance
|
24 |
|
25 | ```js
|
26 | // store.js
|
27 | import Vue from 'vue'
|
28 | import Puex from 'puex'
|
29 |
|
30 | Vue.use(Puex)
|
31 |
|
32 | const store = new Puex({
|
33 | state: {},
|
34 | mutations: {},
|
35 | actions: {},
|
36 | plugins: []
|
37 | })
|
38 | ```
|
39 |
|
40 | ### Bind store instance
|
41 |
|
42 | In order to access `store` in every component:
|
43 |
|
44 | ```js
|
45 | import Vue from 'vue'
|
46 | import store from './store'
|
47 |
|
48 | // In your root Vue instance:
|
49 | new Vue({
|
50 | store,
|
51 | render: h => h(YourApp)
|
52 | })
|
53 | // this.$store will be available in component
|
54 | ```
|
55 |
|
56 | ### store
|
57 |
|
58 | - store.state `readonly`
|
59 | - store.mutations
|
60 | - store.actions
|
61 | - store.commit(type, payload)
|
62 | - store.dispatch(type, payload)
|
63 | - store.subscribe(subscriber)
|
64 | - store.replaceState(newState)
|
65 | - store.mapState(map)
|
66 | - store.mapActions(map)
|
67 | - store.mapMutations(map)
|
68 |
|
69 | ## API
|
70 |
|
71 | ### Constructor
|
72 |
|
73 | ```js
|
74 | const store = new Puex({ state, mutations, actions })
|
75 | ```
|
76 |
|
77 | #### state
|
78 |
|
79 | `state` is nothing special, it can be either an object or a function that returns an object, then you can access it via `store.state` which will be read-only, to replace root state you can use `store.replaceState(newState)` instead.
|
80 |
|
81 | #### mutations
|
82 |
|
83 | ```js
|
84 | const mutations = {
|
85 | INCREMENT(state, amount = 1) {
|
86 | state.count += amount
|
87 | }
|
88 | }
|
89 |
|
90 | store.commit('INCREMENT', 10)
|
91 | ```
|
92 |
|
93 | > **NOTE:** You can only mutate state inside a *mutation*.
|
94 |
|
95 | #### actions
|
96 |
|
97 | ```js
|
98 | const actions = {
|
99 | incrementAsync(store, id) {
|
100 | return getAmountByIdAsync(id).then(amount => {
|
101 | store.commit('INCREMENT', amount)
|
102 | })
|
103 | }
|
104 | }
|
105 |
|
106 | store.dispatch('incrementAsync', 42)
|
107 | ```
|
108 |
|
109 | #### plugins
|
110 |
|
111 | ```js
|
112 | const loggerPlugin = store => {
|
113 | store.subscribe(mutation => {
|
114 | console.log(mutation)
|
115 | })
|
116 | }
|
117 |
|
118 | new Puex({
|
119 | plugins: [loggerPlugin]
|
120 | })
|
121 | ```
|
122 |
|
123 | ### store.subscribe(subscriber)
|
124 |
|
125 | ```js
|
126 | const unsubscribe = store.subscribe((mutation, state) => {
|
127 | console.log(mutation.type)
|
128 | console.log(mutation.payload)
|
129 | })
|
130 | ```
|
131 |
|
132 | ### store.replaceState(newState)
|
133 |
|
134 | Replace root state.
|
135 |
|
136 | ## Contributing
|
137 |
|
138 | 1. Fork it!
|
139 | 2. Create your feature branch: `git checkout -b my-new-feature`
|
140 | 3. Commit your changes: `git commit -am 'Add some feature'`
|
141 | 4. Push to the branch: `git push origin my-new-feature`
|
142 | 5. Submit a pull request :D
|
143 |
|
144 |
|
145 | ## Author
|
146 |
|
147 | **puex** © [egoist](https://github.com/egoist), Released under the [MIT](./LICENSE) License.<br>
|
148 | Authored and maintained by egoist with help from contributors ([list](https://github.com/egoist/puex/contributors)).
|
149 |
|
150 | > [egoistian.com](https://egoistian.com) · GitHub [@egoist](https://github.com/egoist) · Twitter [@_egoistlily](https://twitter.com/_egoistlily)
|