Sometimes, we want to access store state in React Redux and JavaScript.
In this article, we’ll look at how to access store state in React Redux and JavaScript.
How to access store state in React Redux and JavaScript?
To access store state in React Redux and JavaScript, we can use the useSelector hook.
For instance, we write
import { useSelector } from "react-redux";
export const useEmployees = () => {
return useSelector((state) => state.employees);
};
to create the useEmployees hook.
In it, we call the useSelector hook that select the employees state from the Redux store.
Then, we write
const Comp = () => {
const { employees } = useEmployees();
//...
};
to call the useEmployees hook in the Comp component to get the employees from the store.
Conclusion
To access store state in React Redux and JavaScript, we can use the useSelector hook.