Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wellguimaraes/stateware
Smart state container with easy copy and auto memoized getters
https://github.com/wellguimaraes/stateware
getters redux state stores
Last synced: 6 days ago
JSON representation
Smart state container with easy copy and auto memoized getters
- Host: GitHub
- URL: https://github.com/wellguimaraes/stateware
- Owner: wellguimaraes
- License: mit
- Created: 2017-03-13T14:10:07.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-03-09T05:19:41.000Z (over 6 years ago)
- Last Synced: 2024-09-22T00:42:47.461Z (about 2 months ago)
- Topics: getters, redux, state, stores
- Language: JavaScript
- Size: 88.9 KB
- Stars: 20
- Watchers: 5
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ![Actionware](assets/logo.png)
A fast, dependency-free state container with easy copy and automagically memoized getters, designed for immutability.###### Extra power
Wanna reduce Redux boilerplate? Use it combined with **[Actionware](https://github.com/wellguimaraes/actionware)** lib.## Install it
`npm i stateware --save``yarn add stateware`
## Use it
```js
import { createState } from 'stateware'const initialState = createState({
users: [],
genderFilter: null,
filteredUsers({ users, genderFilter }) {
return users.filter(user => !genderFilter || user.gender === genderFilter);
},
totalPosts({ filteredUsers }) {
return filteredUsers.reduce((sum, user) => sum + user.postsCount, 0);
}
})// Create a new state, updating 'users' value
const newState = initialState.copy({
users: [
{ name: 'John Doe' , gender: 'M', postsCount: 10 },
{ name: 'Jane Doe' , gender: 'F', postsCount: 5 },
{ name: 'Alan Turing' , gender: 'M', postsCount: 15 },
{ name: 'Ada Lovelace' , gender: 'F', postsCount: 16 }
]
})// Access state values
newState.users
newState.genderFilter
newState.filteredUsers
newState.totalPosts```
### Usage with Redux
Do this:
```js
switch (action.type) {
case 'SET_GENDER_FILTER':
return state.copy({
genderFilter: action.payload
})
}
```Instead of:
```
switch (action.type) {
case 'SET_GENDER_FILTER':
const filter = action.payload
return {
...state,
genderFilter: filter,
filteredUsers: state.users.filter(user => !filter || user.gender === filter)
}
}
```## License
[MIT](LICENSE) © Wellington Guimaraes