<aside> 💻 Want to test it out?
All the code in this example is available for you to test in REPL.it.
</aside>
Below, we'll walk through an introductory Riduce with Redux example, showing:
Let's imagine we're controlling the state for a museum.
import { createStore } from 'redux'
import riduce from 'riduce' // 1st line: import
const museumState = {
isOpen: false,
visitor: {
counter: 0,
guestbook: ['richard woz here']
}
}
const [reducer, actions] = riduce(museumState) // 2nd line: setup
const { getState, dispatch } = createStore(reducer)
And that's it. Those two lines replace all of our reducer boilerplate.
Continuing on from above, let's:
Previously, you might create 4 x reducer branches, action types and action creators.
Riducer gets rid of all that boilerplate.
Now, it's as simple as describing the changes we want to see!
// at `state.isOpen`, create an action to toggle the boolean
dispatch(actions.isOpen.create.toggle())
// at `state.visitor.counter`, create an action to add 5
dispatch(actions.visitor.counter.create.increment(5))
// at `state.visitor.guestbook`, create an action to push a string
dispatch(actions.visitor.guestbook.create.push('LOL from js fan'))
// at `state.visitor.guestbook[0]`, create an action to concat a string
dispatch(actions.visitor.guestbook[0].create.concat('!!!'))
getState()
/*
{
isOpen: true,
visitor: {
counter: 5,
guestbook: [
'richard woz here!!!',
'LOL from js fan'
]
}
}
*/