In Redux, data is stored in a central store, and components read data from the store using selectors.
✅ 1️⃣ Using useSelector Hook (Modern & Most Common)
import { useSelector } from "react-redux";
function Profile() {
const user = useSelector(state => state.user);
return <h2>{user.name}</h2>;
}
🧠 Explanation
useSelectorsubscribes to the Redux store- It selects the required slice of state
- Component re-renders only when selected data changes
✅ 2️⃣ Using connect (Older / Class Components)
import { connect } from "react-redux";
const Profile = ({ user }) => <h2>{user.name}</h2>;
const mapStateToProps = (state) => ({
user: state.user
});
export default connect(mapStateToProps)(Profile);
🧠 Explanation
mapStateToPropsmaps store state to propsconnectinjects data into the component
🔹 3️⃣ Using Selectors (Best Practice)
const selectUserName = (state) => state.user.name;
const name = useSelector(selectUserName);
✔ Reusable
✔ Cleaner
✔ Easy to test
🎯 Short Interview Answer
We get data from the Redux store using
useSelectorin functional components orconnectwithmapStateToPropsin class components. Selectors are preferred for cleaner and reusable access to state.
⭐ One-line summary
Redux data is accessed using selectors—most commonly with useSelector.