Free Ebook cover Advanced React JS: State Management with Redux

Advanced React JS: State Management with Redux

New course

119 pages

Integrating Redux with React Components

Capítulo 49

Estimated reading time: 8 minutes

Audio Icon

Listen in audio

0:00 / 0:00

Integrating Redux with React components is a crucial step in building scalable and maintainable React applications. Redux is a predictable state container for JavaScript apps, and it works particularly well with React due to its component-based architecture. By integrating Redux, you can manage your application's state more effectively, making it easier to debug and test.

To begin the integration of Redux with React components, you first need to understand the core concepts of Redux: actions, reducers, and the store.

Actions

Actions are payloads of information that send data from your application to your Redux store. They are the only source of information for the store. You send them to the store using store.dispatch(). Actions are plain JavaScript objects that must have a type property to indicate the type of action being performed. Additional data can be included as needed.

const addTodo = (text) => {
  return {
    type: 'ADD_TODO',
    payload: text
  };
};

Reducers

Reducers specify how the application's state changes in response to actions sent to the store. They are pure functions that take the previous state and an action, and return the next state. It’s important to return new state objects, rather than mutating the previous state.

const todos = (state = [], action) => {
  switch (action.type) {
    case 'ADD_TODO':
      return [...state, action.payload];
    default:
      return state;
  }
};

The Store

The store is the object that brings actions and reducers together. The store has the following responsibilities:

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

  • Holds application state
  • Allows access to state via getState()
  • Allows state to be updated via dispatch(action)
  • Registers listeners via subscribe(listener)
  • Handles unregistering of listeners via the function returned by subscribe(listener)
import { createStore } from 'redux';
import rootReducer from './reducers';

const store = createStore(rootReducer);

Connecting React Components to Redux

To connect Redux with React components, you use the react-redux library, which provides the Provider component and the connect function.

Provider

The Provider component makes the Redux store available to any nested components that need to access the Redux store. It is used to wrap the root component of your React application.

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import App from './App';
import store from './store';

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
);

Connect

The connect function is used to connect a React component to the Redux store. It allows you to map state and dispatch to the props of your component.

import React from 'react';
import { connect } from 'react-redux';

const TodoList = ({ todos }) => (
  <ul>
    {todos.map((todo, index) => (
      <li key={index}>{todo}</li>
    ))}
  </ul>
);

const mapStateToProps = (state) => {
  return {
    todos: state.todos
  };
};

export default connect(mapStateToProps)(TodoList);

Dispatching Actions

To change the state, you must dispatch actions. The connect function can also map dispatch to props, allowing you to call action creators directly from your component.

import React from 'react';
import { connect } from 'react-redux';
import { addTodo } from './actions';

const AddTodo = ({ dispatch }) => {
  let input;

  return (
    <div>
      <form
        onSubmit={e => {
          e.preventDefault();
          if (!input.value.trim()) {
            return;
          }
          dispatch(addTodo(input.value));
          input.value = '';
        }}
      >
        <input ref={node => (input = node)} />
        <button type="submit">
          Add Todo
        </button>
      </form>
    </div>
  );
};

export default connect()(AddTodo);

Using Hooks with Redux

With the introduction of React hooks, you can now use Redux with hooks, which can simplify the code and make it more readable. The useSelector hook allows you to extract data from the Redux store state, while the useDispatch hook gives you access to the dispatch function.

import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { addTodo } from './actions';

const TodoApp = () => {
  const todos = useSelector(state => state.todos);
  const dispatch = useDispatch();
  let input;

  const handleSubmit = (e) => {
    e.preventDefault();
    if (!input.value.trim()) {
      return;
    }
    dispatch(addTodo(input.value));
    input.value = '';
  };

  return (
    <div>
      <form onSubmit={handleSubmit}>
        <input ref={node => (input = node)} />
        <button type="submit">
          Add Todo
        </button>
      </form>
      <ul>
        {todos.map((todo, index) => (
          <li key={index}>{todo}</li>
        ))}
      </ul>
    </div>
  );
};

export default TodoApp;

By using hooks, you can streamline your component logic and remove the need for connect. This can lead to more concise and understandable components.

Conclusion

Integrating Redux with React components provides a powerful way to manage state in your applications. By understanding and utilizing actions, reducers, the store, and the react-redux library, you can build applications that are easier to maintain and scale. Additionally, with the advent of React hooks, working with Redux has become even more intuitive, allowing for cleaner and more functional component logic.

As you continue to develop with Redux and React, remember that the key to effectively managing state lies in keeping your reducers pure, your actions clear, and your components connected in a way that enhances the flow of data and logic throughout your application.

Now answer the exercise about the content:

What is the primary role of actions in a Redux application?

You are right! Congratulations, now go to the next page

You missed! Try again.

Actions in Redux are payloads of information that send data from your application to your Redux store. They are the only source of information for the store. By using actions to dispatch updates, the application’s state can be managed in an organized and predictable manner.

Next chapter

Handling Side Effects in Redux

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.