{
type: ADD_TODO,
text: 'Build my first Redux app'
}
{
type: COMPLETE_TODO,
index: 5
}
function addTodo(text) {
return {
type: ADD_TODO,
text
}
}
function (currentState, action) {
//...
return newState
}
// ES6 default arguments syntax
function reducer(state = initialState, action) {
switch (action.type) {
case SET_VISIBILITY_FILTER:
return Object.assign({}, state, {
visibilityFilter: action.filter
})
default:
return state
}
}
function doSomething(state, action) {
}
function doSomethingElse(state, action) {
}
function reducer(state, action) {
a: doSomething(state, action),
b: doSonethingElse(state, action)
}
//or
var reducer = combineReducers({
a: doSomething,
b: doSonethingElse
})
import { createStore } from 'redux'
import reducer from './reducers'
let store = createStore(reducer)
// Log the initial state
console.log(store.getState())
// Every time the state changes, log it
// Note that subscribe() returns a function for unregistering the listener
let unsubscribe = store.subscribe(() =>
console.log(store.getState())
)
// Dispatch some actions
store.dispatch(addTodo('Learn about actions'))
store.dispatch(addTodo('Learn about reducers'))
store.dispatch(addTodo('Learn about store'))
store.dispatch(completeTodo(0))
store.dispatch(completeTodo(1))
store.dispatch(setVisibilityFilter(VisibilityFilters.SHOW_COMPLETED))
// Stop listening to state updates
unsubscribe()
mapStateToProps = (state) => {
return {
a: state.something
}
}
mapDispatchToProps = (dispatch) => {
return {
onClick: (id) => {
dispatch(action(id))
}
}
}
wrappedCompoent = connect(mapStateToProps, mapDispatchToProps)(component)