12 import { createStore } from 'redux'
18 function counter (state = { value: 0 }, action) {
19 switch (action.type) {
21 return { value: state.value + 1 }
23 return { value: state.value - 1 }
31 let store = createStore(counter)
35 // Optional - you can pass `initialState` as a second arg
36 let store = createStore(counter, { value: 0 })
39 A store is made from a reducer function, which takes the current `state`, and
40 returns a new `state` depending on the `action` it was given.
45 let store = createStore(counter)
50 // Dispatches an action; this changes the state
51 store.dispatch({ type: 'INCREMENT' })
52 store.dispatch({ type: 'DECREMENT' })
56 // Gets the current state
61 // Listens for changes
62 store.subscribe(() => { ... })
65 Dispatch actions to change the store's state.
72 import { Provider } from 'react-redux'
78 <Provider store={store}>
80 </Provider>, mountNode)
83 The `<Provider>` component makes the store available in your React components. You need this so you can use `connect()`.
88 import { connect } from 'react-redux'
93 // A functional React component
94 function App ({ message, onMessageClick }) {
96 <div onClick={() => onMessageClick('hello')}>
104 // Maps `state` to `props`:
105 // These will be added as props to the component.
106 function mapState (state) {
107 return { message: state.message }
110 // Maps `dispatch` to `props`:
111 function mapDispatch (dispatch) {
113 onMessageClick (message) {
114 dispatch({ type: 'click', message })
120 export default connect(mapState, mapDispatch)(App)
126 export default connect(
128 message: state.message
131 onMessageClick: (message) => {
132 dispatch({ type: 'click', message })
138 Same as above, but shorter.
140 ### Combining reducers
143 const reducer = combineReducers({
148 Combines multiple reducers into one reducer function. See: [combineReducers](https://redux.js.org/docs/api/combineReducers.html) _(redux.js.org)_
156 const logger = store => dispatch => action { dispatch(action) }
160 const logger = store => {
161 // This function runs on createStore().
162 // It returns a decorator for dispatch().
165 // Runs on createStore(), too.
166 // It returns a new dispatch() function
169 // Runs on every dispatch()
175 Middlewares are simply decorators for `dispatch()` to allow you to take
176 different kinds of actions, and to perform different tasks when receiving
179 ### Applying middleware
182 const enhancer = applyMiddleware(logger, thunk, ...)
186 const store = createStore(reducer, {}, enhancer)
193 * [Redux](https://www.npmjs.com/package/redux) _(npmjs.com)_
194 * [React-redux](https://www.npmjs.com/package/react-redux) _(npmjs.com)_
195 * [Usage with React](http://redux.js.org/docs/basics/UsageWithReact.html) _(redux.js.org)_