OiO.lk Blog javascript React Redux access full store state via hook like useSelector
javascript

React Redux access full store state via hook like useSelector


I’m learning Redux to use it with React. And I’m stuck with a simple task.
I have a store which consists of an array of objects like this

const initialState = {
  items: [
    {
      property1: someValue,
      property2: someValue,
      ...
    },
    {
      property1: someValue,
      property2: someValue,
      ...
    },
    {
      property1: someValue,
      property2: someValue,
      ...
    },
    ...
  ]
}

I’ve set up Provider, store, sliceReducer, and all other necessary stuff.

At one place in my app I need to get a particular item by Id. So I’ve written a selector function for this and using it with a useSelector hook. And it works nice.

But at the other place I need to get whole array of objects. This means I need to get a full store state. And selector functions are only for selecting some piece of the store.

I thought that calling useSelector hook without parameters will give me whole that state object I’m getting in selector functions. However it throws an error that I must pass a selector to useSelector.

I know that there is the useStore hook exists but I’ve read that it gives a reference to store without rerenders. So it means that this hook does not update this reference when the store state changes.

How can I get full store state in my React component using hook like useSelector with rerenders?



You need to sign in to view this answers

Exit mobile version