Next.js Discord

Discord Forum

Redux

Unanswered
Maltese posted this in #help-forum
Open in Discord
MalteseOP
hello, folks! Can somebody help me with this? The problem is related to Redix. Here I have a useSelector hook that runs 4 times? Why is that so?
const Category = () => {
  console.log('MyComponent rendered');

  const { category } = useParams();
  const categoriesMap = useSelector(selectProductsByCategory);
  return (
    <Fragment>
      <Title>{category!.toUpperCase()}</Title>
      <CategoryContainer>
        {/* {products?.length > 0 ? (
          products.map((product:TProduct) => (
            <ProductCard key={product.id} product={product} />
          ))
        ) : ( )}*/}
        <span>There are no products left</span>
      </CategoryContainer>
    </Fragment>
  );
};

1 Reply

MalteseOP
I figure out why. when useSelector hook is called it checks two times the returned value, thats why there are the first two categoriesArr = [] ([] is the initail value), after that I have a user dispatch that is called and it triggers all selectors, that why there is the third categoriesArr=[] and the last is because after the user dispatch I also have categories dispatch that also triggers the selectors