Connecting Redux with React Components
Theory
To establish a connection between React components and the Redux store, we can use the useSelector hook to access the state and the useDispatch hook for dispatching actions.
Practice
Now, let's connect the Counter component to the Redux store.
import React from "react";
import { useSelector, useDispatch } from "react-redux";
import { increment, decrement } from "../redux/actions/counterAction";
const Counter = () => {
const counter = useSelector((state) => state.counter);
const dispatch = useDispatch();
const handleIncrement = () => {
dispatch(increment());
};
const handleDecrement = () => {
dispatch(decrement());
};
return (
<div>
<h2>Counter Value: {counter}</h2>
<button onClick={handleIncrement}>Increment</button>
<button onClick={handleDecrement}>Decrement</button>
</div>
);
};
export default Counter;
Code explanation:
- Line 2: Import the necessary hooks
useSelectoranduseDispatchfrom thereact-reduxlibrary. These hooks enable the connection between React and Redux; - Line 3: Import the
incrementanddecrementaction creators from the../redux/actions/counterActionfile. These actions will be dispatched to update the counter state in the Redux store; - Line 6: The
useSelectorhook is used to access thecounterstate from the Redux store. We provide a selector function as an argument, which returns the desired state value; - Line 7: The
useDispatchhook is used to access Redux'sdispatchfunction. It refers to thedispatchfunction, which is used to dispatch actions to the Redux store; - Line 9-11: Define the event handler
handleIncrementthat is called when the "Increment" button is clicked. We dispatch theincrementaction to the Redux store inside the handler using thedispatchfunction; - Line 13-15: Define the event handler
handleDecrementthat is called when the "Decrement" button is clicked. We dispatch thedecrementaction inside the handler to the Redux store using thedispatchfunction; - Line 17-23: Render the JSX elements, including the current counter value and buttons for incrementing and decrementing. We attach the respective event handlers to the buttons'
onClickattribute.
Note
This code connects a React component (
Counter) to Redux. It usesuseSelectorto access the counter state anduseDispatchto dispatch actions for state updates. The component displays the counter value and provides buttons to increment and decrement it. Clicking these buttons triggers actions that update the Redux store.
1. How do you establish a connection between React components and the Redux store for accessing state?
2. What is the purpose of the useSelector hook in the code?
3. What does the handleDecrement function do when called?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain how the `useSelector` hook works in this example?
What happens if I click the increment or decrement button?
Can you describe what the `dispatch` function does in this code?
Awesome!
Completion rate improved to 4.17
Connecting Redux with React Components
Swipe to show menu
Theory
To establish a connection between React components and the Redux store, we can use the useSelector hook to access the state and the useDispatch hook for dispatching actions.
Practice
Now, let's connect the Counter component to the Redux store.
import React from "react";
import { useSelector, useDispatch } from "react-redux";
import { increment, decrement } from "../redux/actions/counterAction";
const Counter = () => {
const counter = useSelector((state) => state.counter);
const dispatch = useDispatch();
const handleIncrement = () => {
dispatch(increment());
};
const handleDecrement = () => {
dispatch(decrement());
};
return (
<div>
<h2>Counter Value: {counter}</h2>
<button onClick={handleIncrement}>Increment</button>
<button onClick={handleDecrement}>Decrement</button>
</div>
);
};
export default Counter;
Code explanation:
- Line 2: Import the necessary hooks
useSelectoranduseDispatchfrom thereact-reduxlibrary. These hooks enable the connection between React and Redux; - Line 3: Import the
incrementanddecrementaction creators from the../redux/actions/counterActionfile. These actions will be dispatched to update the counter state in the Redux store; - Line 6: The
useSelectorhook is used to access thecounterstate from the Redux store. We provide a selector function as an argument, which returns the desired state value; - Line 7: The
useDispatchhook is used to access Redux'sdispatchfunction. It refers to thedispatchfunction, which is used to dispatch actions to the Redux store; - Line 9-11: Define the event handler
handleIncrementthat is called when the "Increment" button is clicked. We dispatch theincrementaction to the Redux store inside the handler using thedispatchfunction; - Line 13-15: Define the event handler
handleDecrementthat is called when the "Decrement" button is clicked. We dispatch thedecrementaction inside the handler to the Redux store using thedispatchfunction; - Line 17-23: Render the JSX elements, including the current counter value and buttons for incrementing and decrementing. We attach the respective event handlers to the buttons'
onClickattribute.
Note
This code connects a React component (
Counter) to Redux. It usesuseSelectorto access the counter state anduseDispatchto dispatch actions for state updates. The component displays the counter value and provides buttons to increment and decrement it. Clicking these buttons triggers actions that update the Redux store.
1. How do you establish a connection between React components and the Redux store for accessing state?
2. What is the purpose of the useSelector hook in the code?
3. What does the handleDecrement function do when called?
Thanks for your feedback!