React-Redux (connect) Tutorial 🎯

beginner
25 min

React-Redux (connect) Tutorial 🎯

Welcome back to CodeYourCraft! Today, we're diving into the world of React-Redux, a powerful combination that helps manage state in complex React applications. If you're new to Redux, don't worryβ€”we'll start from the ground up!

What is Redux? πŸ“

Redux is a predictable state container for JavaScript applications. It helps you write applications that behave consistently, run in different environments (like a web browser or on the server), and are easy to test.

Why Use Redux with React? πŸ’‘

React itself manages the state of small components, but for larger applications, it can become difficult to manage state across multiple components. This is where Redux shinesβ€”it allows you to store the entire state of your application in a single place, making it easier to reason about and manage.

Installing Redux and React-Redux βœ…

To get started with Redux in your React project, you'll first need to install Redux and React-Redux:

bash
npm install redux react-redux

Creating a Store πŸ“

The Redux Store is where your application's state lives. Let's create a store for our simple to-do application.

javascript
import { createStore } from 'redux'; // Root Reducer const rootReducer = (state = {}, action) => { switch (action.type) { // Handling actions here default: return state; } }; // Creating Store const store = createStore(rootReducer); export default store;

Understanding Actions πŸ“

Actions are the events that trigger state changes in your application. They're plain JavaScript objects with a type property that identifies the action being taken.

javascript
// ADD_TODO action { type: 'ADD_TODO', id: 0, text: 'Learn about Redux' }

Creating a Reducer πŸ“

A Reducer is a pure function that takes the previous state and an action, and returns the new state.

javascript
// Reducer for ADD_TODO action function todos(state = [], action) { switch (action.type) { case 'ADD_TODO': return [...state, { id: action.id, text: action.text, completed: false }]; // Handling other actions here default: return state; } }

Combining Reducers πŸ“

In larger applications, you'll often have multiple reducers for different parts of your application's state. You can combine them using combineReducers from Redux.

javascript
import { createStore, combineReducers } from 'redux'; const rootReducer = combineReducers({ todos: todos });

Using connect πŸ’‘

connect is a function provided by React-Redux that lets you connect your React components to your Redux store.

javascript
import { connect } from 'react-redux'; const TodoList = ({ todos }) => ( <ul> {todos.map(todo => ( <li key={todo.id}> {todo.text} </li> ))} </ul> ); const mapStateToProps = state => ({ todos: state.todos }); export default connect(mapStateToProps)(TodoList);

Dispatching Actions πŸ’‘

Finally, to dispatch an action, you can use the dispatch function that comes with your connected component.

javascript
import React from 'react'; import { addTodo } from './actions'; class TodoApp extends React.Component { addTodo = () => { this.props.dispatch(addTodo()); }; render() { return ( <div> <h3>My To-do App</h3> <button onClick={this.addTodo}>Add Todo</button> <TodoList todos={this.props.todos} /> </div> ); } } export default connect(mapStateToProps)(TodoApp);

Putting It All Together 🎯

Now that you understand the basics, let's put everything together and create a simple to-do app. You can find the complete code here.

Quiz Time 🎯

Quick Quiz
Question 1 of 1

What does Redux help you achieve in a React application?

Quick Quiz
Question 1 of 1

What is a Redux Store?