Reselect: Reselect + react/redux useSelector() doesn't work as intended

Created on 26 Dec 2019  路  3Comments  路  Source: reduxjs/reselect

https://react-redux.js.org/next/api/hooks#using-memoizing-selectors

Hello,

If I create a _ShopCartList_ component:

`

export default () => {
const cartItems = useSelector(selectShopCartItems);

 console.log("I'm rerendering even on unrelated state changes");

return (
    cartItems ?
        <ul className="shop-cart-list">
            {
                Object.entries(cartItems).map(([id, item]) => (
                    <li className="shop-cart-list__item-wrapper" key={id}>
                        <ShopCartListItem item={item}/>
                    </li>
                ))
            }
        </ul>
        :
        null
);

}

`

where _selectShopCartItems_ is:

`const selectShopCart = state => state.shopCart;

export const selectShopCartItems = createSelector(
[selectShopCart],
shopCart => shopCart.cartItems
);`

_ShopCartList_ component will still rerender on every unrelated state changes.

Most helpful comment

I just found that the official react-redux useSelector docs have really good, thoroughly explained examples of usage with reselect: https://react-redux.js.org/next/api/hooks#useselector-examples

All 3 comments

Please create a CodeSandbox project that demonstrates the issue.

I am also adding items in a cart using useSelector and facing the same issue. @maxim1006 have you solved this issue yet? I don't think we need to use reselect library useMemo or useCallback might get the work done.

I just found that the official react-redux useSelector docs have really good, thoroughly explained examples of usage with reselect: https://react-redux.js.org/next/api/hooks#useselector-examples

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jEnbuska picture jEnbuska  路  6Comments

clementdevos picture clementdevos  路  4Comments

alvaromb picture alvaromb  路  5Comments

Rastamanby picture Rastamanby  路  4Comments

Nemsae picture Nemsae  路  8Comments