Skip to content

Instantly share code, notes, and snippets.

@asm-jaime
Last active August 8, 2016 23:56
Show Gist options
  • Save asm-jaime/0986b31783799e6f6424120c28c3acfe to your computer and use it in GitHub Desktop.
Save asm-jaime/0986b31783799e6f6424120c28c3acfe to your computer and use it in GitHub Desktop.
redux, increments decrements example
import {
createStore
}
from 'redux'
//начальное состояние хранилища
//нужно положить в аргумент state у reducer
let defaultState = {
numState: 0,
strState: "nothing to report",
items: []
}
//редуктор (изменения состояния хранилища в текущем случае)
//нужно положить в createStore
//вызывается после dispatch
let reducer = (state = defaultState, action) => {
switch (action.type) {
case 'INCREMENT':
state.numState++;
state.strState = "incrementation";
state.items.push(action.data);
return state;
case 'DECREMENT':
state.numState--;
state.strState = "decrementation";
state.items.push(action.data);
return state;
default:
return state;
}
}
//создать хранилище
let store = createStore(reducer)
//функция изменения состояния
//нужно положить в dispatch
//вызывается в dispatch
let incrementation = (obj) => {
return {
type: 'INCREMENT',
data: obj
}
}
//функция изменения состояния
//нужно положить в dispatch
//вызывается в dispatch
let decrementation = (obj) => {
return {
type: 'DECREMENT',
data: obj
}
}
//реакция на изменение состояния(наблюдатель)
//вызывается после reducer
store.subscribe(() =>
console.log(store.getState());
)
//вызов изменения состояния
store.dispatch(incrementation("inc"));
store.dispatch(decrementation("dec"));
store.dispatch(decrementation("dec"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment