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
  • Counter Example
  • Full Example

Was this helpful?

  1. Redux

Using Redux with Components

Instead of having to manage the store subscribtions manually we can use react-redux to connect our store to React components. To demonstrate how this works, let's take a look at the counter example.

Counter Example

We start by building out a counter component. The component will be responsible for keeping track of how many times it was clicked, and displaying the amount.

import React from 'react';
import { connect } from 'react-redux';
import { increase, decrease } from '../reducers/counter';


function mapStateToProps(state) {
  return {
    counter: state.counter,
  };
}

function mapDispatchToProps(dispatch) {
  return {
    onIncrease: () => dispatch(increase()),
    onDecrease: () => dispatch(decrease()),
  };
}

const Counter = ({ onIncrease, onDecrease, counter }) => {
  return (
    <div>
      <pre>{ counter }</pre>
      <button onClick={ onIncrease }>Increase</button>
      <button onClick={ onDecrease }>Decrease</button>
    </div>
  );
};

const App = connect(
  mapStateToProps,
  mapDispatchToProps,
)(Counter);

ReactDOM.render(
  App,
  document.getElementById('root')
);

The template syntax should be familiar by now, displaying a counter value, and handling some click events. Lets take a look at the use of connect.

  • mapStateToProps: connect will subscribe to Redux store updates. Any time it updates, mapStateToProps will be called. It's result must be a plain object. Which will then be passed to the component as props.

Full Example

git checkout 04a-redux
jspm install
npm run dev
PreviousConfiguring your Application to use ReduxNextRedux and Component Architecture

Last updated 6 years ago

Was this helpful?

mapDispatchToProps: Optional. The store's dispatch method is passed in as an argument here. You can then use that to wrap your actions or pass dispatch into them. The result of this function is also passed into the component as props. Tip: you could use the helper from Redux to simplify this.

bindActionCreators()