Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/egoist/zerotwo
😈 Refined state containter for Vue.js
https://github.com/egoist/zerotwo
state-management vue
Last synced: 16 days ago
JSON representation
😈 Refined state containter for Vue.js
- Host: GitHub
- URL: https://github.com/egoist/zerotwo
- Owner: egoist
- Created: 2018-02-01T15:40:34.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2021-12-25T13:49:00.000Z (almost 3 years ago)
- Last Synced: 2024-10-23T12:18:45.418Z (24 days ago)
- Topics: state-management, vue
- Language: JavaScript
- Homepage:
- Size: 175 KB
- Stars: 75
- Watchers: 4
- Forks: 6
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Install
```bash
yarn add zerotwo
```CDN: [UNPKG](https://unpkg.com/zerotwo/) | [jsDelivr](https://cdn.jsdelivr.net/npm/zerotwo/) (available as `window.zerotwo`)
## Usage
[![Edit egoist/zerotwo: todomvc](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/egoist/zerotwo/tree/master/examples/todomvc)
Create a `store` first:
```js
import Vue from 'vue'
import zerotwo, { createStore } from 'zerotwo'Vue.use(zerotwo)
const store = createStore({
state: { count: 0 },
mutations: {
increment: state => state.count++
}
})new Vue({
store,
render: h => h(App)
})
```Then create your `App`:
```js
import { connect, state, mutation } from 'zerotwo'// A "stateless" component
const Counter = {
props: ['count', 'increment'],
template: `{{ count }}`
}// Connect needed state and mutations to `Counter`
const App = {
template: '',
components: {
Counter: connect({
count: state(),
increment: mutation()
}, Counter)
}
}export default App
```It's almost the same as `Vuex` but here's no `mapState` `mapMutations` etc.. Instead you use `connect` to feed any needed data to your component as props.
### connect
```js
import { state, getter, action, mutation } from 'zerotwo'connect({
count: state(),
doubleCount: getter(),
increment: mutation(),
incrementAsync: action()
})// To connect from a different name
// Just pass the name to the connect helpers like:
connect({
// state.thatCount -> this.count
count: state('thatCount')
})
```### createStore({ state, mutations, actions, getters, plugins })
#### state
Type: `Function | object`
Initial state.
#### mutations
Type: `{ [type: string]: Function }`
Mutation handlers.
#### actions
Type: `{ [type: string]: Function }`
Action handlers.
#### getters
Type: `{ [key: string]: Function }`
Register getters on the store. The getter function receives the following arguments:
```js
state,
getters
```#### plugins
Type: `Array`
An array of plugin functions to be applied to the store. The plugin simply receives the store as the only argument and can either listen to mutations (for outbound data persistence, logging, or debugging) or dispatch mutations (for inbound data e.g. websockets or observables).
### store
#### store.commit(mutation, payload)
#### store.dispatch(action, payload)
#### store.replaceState(newState)
## License
MIT © [EGOIST](https://github.com/egoist)