React Redux Dispatch & Get State 2025
React Redux Dispatch & Get State 2025
In Redux, dispatch is used to send actions to the store, and getState is used to retrieve the current state from the store.
You can dispatch actions using the useDispatch()
hook, and access the current state using the useSelector()
hook in React.
Step 1: Import Hooks from React Redux
import { useDispatch, useSelector } from 'react-redux'
Step 2: Use Dispatch and Selector in Component
const MyComponent = () => {
const dispatch = useDispatch();
const counter = useSelector((state) => state.counter);
const increase = () => {
dispatch({ type: 'INCREMENT' });
}
return (
Count: {counter}
)
}
Step 3: What Happens
- When the button is clicked, dispatch
sends an INCREMENT
action to the store.
- The reducer handles this action and updates the state.
- useSelector
listens for state changes and re-renders the component.
Note:
You must wrap your app in
for Redux to work properly.