Help me with this redux error

I am trying to get products array from state using useSelector hook but it gives me this error plz tell me what am i doing wrong
it gives me the hole state if i remove .products from selector.js

Please post your actual code instead of screenshots. Thank you!

1 Like

I agree with Jeremy - please don’t post pics of code, just cut and paste the code.

But scanning over your code, I suspect it may because you aren’t returning state by default in your reducer - that would cause it to be undefined which would cause the first error that shows up in your console, the “TypeError: Cannot read…” one.

i don’t understand what your saying i am exporting it by default in the reducer plz help
i also tried connect method of react-redux

I’m guessing it’s the missing default case for the switch that just returns the state that is referred to.

oh now i get it thank you

1 Like

Yeah, that was it. Usually in Redux, your reducer would have to have:

  return state;

or some other way that state is always defined and that default it just returns the original state. Remember that the job of the reducer is to return whatever you want the next “state” to be. Every reducer gets run dispatch, and get run as the app boots up, so it’s very important to define the default.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.