Categories
React Answers

How to access store state in React Redux and JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *