DEV Community

Srinu Web developer
Srinu Web developer

Posted on • Originally published at blog.srinudesetti.in

Custom Redux Hooks for Micro Frontends Complete Guide


$ git grep -n "selectIsLoggedIn" | wc -l
80
Enter fullscreen mode Exit fullscreen mode

80 useSelector(selectIsLoggedIn) call sites across 5 federated remotes. Header reads it for the login pill. Cart reads it for the guest-vs-account branch. Checkout reads it for the redirect. Every protected route reads it.

Now imagine the user slice ships a rename: isLoggedIn becomes isAuthenticated. The PATCH bump goes out. Every remote that hasn't redeployed yet starts throwing:

TypeError: Cannot read property 'isLoggedIn' of undefined
Enter fullscreen mode Exit fullscreen mode

80 edits across 5 codebases. Five different teams. One coordinated release window. This is the bug that custom Redux hooks solve.

What the article covers

  • The foundation hooksuseAppDispatch and useAppSelector exported through @myapp/store. Why importing useSelector directly from react-redux silently breaks the singleton.
  • Three legitimate hook shapes: read-only (useIsLoggedIn), read+write (useCurrentUser, useCart), view-model (useHeaderViewModel — composes 3 slices via createSelector).
  • Two re-render traps that look correct at code review: selectors defined inside the hook body, hooks returning new object literals without useMemo.
  • Folder layout at packages/core/store/src/hooks/ — flat, one file per hook, every hook re-exported through index.js.
  • renderHook testing pattern with a fresh makeStore() per test. The stability test (result.current.login keeps same reference across rerenders) catches the #1 silent regression.

Builds directly on top of the slices article — these hooks are the consumer API for the slice patterns from there.

Read the full guide with code examples → https://blog.srinudesetti.in/micro-frontend/state-management/custom-redux-hooks-micro-frontend

Top comments (0)