Rangle.io: React Training
gitbook
gitbook
  • Introduction
  • Setup
  • Functional JavaScript
  • ES6 constructs
    • Default Params
    • Template Literals
    • Destructuring
    • Arrow Functions
    • Promises
    • let and const
    • Modules
  • Thinking in Components
    • Atomic Design
    • Atomic Component Principles
    • Benefits of This Approach
    • The Process
    • Task #1
  • React Components
    • Stateless Components
    • Stateful Components
    • Stateful vs Stateless Components
    • Composition
    • Task #2
    • Task #3
    • Task #4
    • Task #5
  • Immutable
    • What Is Immutability?
    • The Case for Immutability
    • JavaScript Solutions
      • Object.assign
      • Object.freeze
    • Immutable.js Basics
      • Immutable.Map
        • Map.merge
      • Nested Objects
        • Deleting Keys
        • Maps are Iterable
      • Immutable.List
      • Performance
      • Persistent and Transient Data Structures
      • Official Documentation
    • Exercises
      • Task #1
      • Task #2
      • Task #3
      • Task #4
      • Task #5
      • Task #6
      • Task #7
  • Redux
    • Review of Reducers and Pure Functions
    • Redux Reducers
    • Redux Actions
    • Configuring your Application to use Redux
    • Using Redux with Components
    • Redux and Component Architecture
  • Routing
    • React Router
    • Router Redux
  • Forms
    • Redux Form
  • Testing
    • Setup
    • Components
    • Reducers
    • Actions
Powered by GitBook
On this page
  • Create Our Application Reducer
  • Create and Configure a Store
  • Subscribe to State Changes
  • Full Example

Was this helpful?

  1. Redux

Configuring your Application to use Redux

Once you have the reducers and actions created, it is time to configure your application to make use of Redux. For this, we will need to:

  • Create our application reducer

  • Create and configure a store

  • Subscribe to the store and update the view

Create Our Application Reducer

import { createStore, combineReducers } from 'redux';

export const INCREASE = '@@reactTraining/INCREASE';
export const DECREASE = '@@reactTraining/DECREASE';

const INITIAL_STATE = 0;

function counterReducer(state = INITIAL_STATE, action = {}) {
  switch (action.type) {
    case INCREASE:
      return state + 1;

    case DECREASE:
      return state - 1;

    default:
      return state;
  }
}

const rootReducer = combineReducers({ counter: counterReducer });

What combineReducers does, is allows us to break out our application into smaller reducers with a single area of concern. Each reducer that you pass into it, will become a property on the state. So when we are subscribing to our state changes, we will be passed in a state object with a property counter, and any other reducers you have provided.

Create and Configure a Store

When creating a store in redux, this is where you provide the middleware you want to use, and the reducer that you want to have for your application.

import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';

//...

const store = compose(
  applyMiddleware(
    thunk
  ),
)(rootReducer);

In this example, we are creating a store that is using the thunk middleware, which will allow our actions to return non-JSON objects such as promises. We could also use other middlewares such as redux-logger, which will provides some logging functionality to the application.

Subscribe to State Changes

Now that we have created our state reducer, and created a store. We now need to subscribe to the store and update our view with the latest state.

//...
store.subscribe(() => {
  ReactDOM.render(
    <div>
      <pre>{ JSON.stringify(store.getState(), null, 2) }</pre>
      <button onClick={ () => store.dispatch(increase()) }>Increase</button>
      <button onClick={ () => store.dispatch(decrease()) }>Decrease</button>
    </div>,
    document.getElementById('root')
  );
});

store.dispatch({ type: 'INIT' });

Full Example

git checkout 04-redux
jspm install
npm run dev
PreviousRedux ActionsNextUsing Redux with Components

Last updated 6 years ago

Was this helpful?