My naive approach to create a state management library.
Inspired by Redux, Vuex and Flux architecture.
npm install lil-store
In all seriousness, just read the code. It's less than 30 lines!
const { createStore } = require('./index')
const reducer = (store, action, payload) => {
if (!store) store = 0 // this is how you define your initial state
switch(action) { // define how your state will change according to action
case 'incr':
store++
break
case 'decr':
store--
break
case 'add':
store += payload
break
}
return store
}
const store = createStore(reducer)
store.subscribe(state => console.log(state))
store.commit('add', 5) // prints out 5
store.commit('incr') // prints out 6
store.commit('incr') // prints out 7
store.commit('decr') // prints out 6
Through UNPKG:
<script src="https://unpkg.com/lil-store"></script>
An object named lilStore
is exposed to the global Window
object.
You can then access createStore
like so:
<script>
var createStore = lilStore.createStore;
</script>
State management is not a React/Vue only need!
This library (more like code snippet) allows you to easily create a reactive store for whatever needs you have!
- Export module for browser use
- Publish that to unpkg.com or something
- Add tests
- Implement TravisCI
- Implement code coverage