Skip to content

Instantly share code, notes, and snippets.

@okunishinishi
Last active May 9, 2017 13:57
Show Gist options
  • Save okunishinishi/5aa6de9ca58c98480c187001867e3a4b to your computer and use it in GitHub Desktop.
Save okunishinishi/5aa6de9ca58c98480c187001867e3a4b to your computer and use it in GitHub Desktop.
Reduxを使ってちゃんとしたアプリをちゃんと作るのがあまりに面倒なんでなんとかしてなんとかする方法を模索してみた ref: http://qiita.com/okunishinishi@github/items/5c4860d3be8cbcc59286
function createCounterWithNamedType(counterName = '') {
return function counter(state = 0, action) {
switch (action.type) {
case `INCREMENT_${counterName}`:
return state + 1;
case `DECREMENT_${counterName}`:
return state - 1;
default:
return state;
}
}
}
function createCounterWithNameData(counterName = '') {
return function counter(state = 0, action) {
const {name} = action;
if(name !== counterName) return state;
switch (action.type) {
case `INCREMENT`:
return state + 1;
case `DECREMENT`:
return state - 1;
default:
return state;
}
}
}
'use strict'
const theStore = require('the-store')
const { Scope } = theStore
// Scoped state class
class CounterScope extends Scope {
// Define initial state
static get initialState () {
return 0
}
// Define reducer factory
static get reducerFactories () {
return {
increment (amount) {
return (state) => state + amount
}
}
}
}
async function tryExample () {
let store = theStore()
// Create state instance and attach to the store
store.load(CounterScope, 'counterA')
store.load(CounterScope, 'counterB')
{
// Access to loaded store
let { counterA } = store
// Each instance has dispatcher methods which share signatures with reducerFactories
counterA.increment(1) // This will dispatch action and reduce into state
// Access to the scoped state
console.log(counterA.state) // -> 1
}
// Store it self has all state
console.log(store.state) // -> { counterA: 1 }
}
tryExample().catch((err) => console.error(err))
class CounterScope extends Scope {
static get reducerFactories () {
return {
increment (amount) {
return (state) => state + amount
}
}
}
}
async function tryExample () {
let store = theStore()
// Create state instance and attach to the store
store.load(CounterScope, 'counterA')
store.load(CounterScope, 'counterB')
}
{
// Access to loaded store
let { counterA } = store
// Each instance has dispatcher methods which share signatures with reducerFactories
counterA.increment(1) // This will dispatch action and reduce into state
// Access to the scoped state
console.log(counterA.state) // -> 1
}
// Store it self has all state
console.log(store.state) // -> { counterA: 1 }
{
"type": "counterA/increment",
"payload": [1]
}
(state) => state + amount`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment