Skip to content

Commit

Permalink
Add mutations section on README
Browse files Browse the repository at this point in the history
  • Loading branch information
Lesnock committed Jul 28, 2021
1 parent 4303979 commit f574af6
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ A reactive store of data for global state management in JS.
- [`has`](#has)
- [`The reactive part`](#the-reactive-part)
- [`listen`](#listen)
- [`Mutations`](#mutations)
- [`Persisting Data`](#persisting-data)
- [`Integration with React`](#using-storem-with-react)
- [`Tutorials`](#tutorials)
Expand Down Expand Up @@ -203,6 +204,31 @@ store.listen('cartItems', (cardItems) => {
})
```
## Mutations
Mutations are functions that can be registered in the store to **alter** his data. It is possible to create a mutation using the **setMutation** method, passing name and function as arguments. The passed function will receive the complete store state as argument. Let's create a mutation that increment a counter, for example:
```javascript
// Create a counter inside the store
store.set({ counter: 1 })

// Create a mutation to increment the counter
store.setMutation('INCREMENT', state => {
state.counter++
})
```
To run a mutation, we use the **runMutation** method passing the mutation name as argument:
```javascript
store.runMutation('INCREMENT') // Increment counter
store.runMutation('INCREMENT') // Increment counter again

console.log(store.get('counter')) // 3
```
**Obs**: Storem **does not** react to data change inside mutations. In others words, listeners will not be executed when data changes occurs inside a mutation.
## Persisting Data
If you want the data to persist throughout the pages, and refreshes,
Expand Down

0 comments on commit f574af6

Please sign in to comment.