Logo

dev-resources.site

for different kinds of informations.

Simple hook to handle featue flags

Published at
3/31/2021
Categories
react
reacthook
customhooks
redux
Author
rafi993
Categories
4 categories in total
react
open
reacthook
open
customhooks
open
redux
open
Author
7 person written this
rafi993
open
Simple hook to handle featue flags

Let's say you want to enable or disable some features based on users role or some permission that the user is granted in the frontend. You could do this with a simple conditional rendering in each component by checking for users permission

<div>
 {role==='guest' && <GuestBanner/>}
</div>
Enter fullscreen mode Exit fullscreen mode

But this is really messy if you have to do this in multiple components. We could instead create a custom hook that returns Boolean saying if we should enable or disable the feature

import { useSelector } from 'react-redux';

const useFeature = (feature) => {

 // If you use something other than redux replace useSelector with equivalent
// getRole is selector to select user role from redux state
const role = useSelector(getRole)

if(feature === 'guestBanner') {
  if(role === 'guest') return true;

  return false;
}

return true;

}
Enter fullscreen mode Exit fullscreen mode

And then in your component you use this as follows

import useFeature from './use-feature';

const GuestBanner = () => {
  const enable = useFeature('guestBanner');

  if(!enable) return null;

   return <h1>Guest Banner</h1>

}
Enter fullscreen mode Exit fullscreen mode
customhooks Article's
29 articles in total
Favicon
Mastering Navigation Control in React with useCallbackPrompt and useBlocker 🚦
Favicon
Custom Hooks in React: Reusing Logic Across Components
Favicon
Fixing UI Update Issues with Custom Hooks in React
Favicon
Custom Hooks in React: A Guide to Creation and Usage
Favicon
React: leveraging custom hooks to extract reusable logic
Favicon
Custom Hooks
Favicon
Custom Hooks in React
Favicon
Mobile Orientation (React-Native)
Favicon
Reusing Logic in React with Custom Hooks: a Practical guide
Favicon
Harnessing Firebase in React with Custom Hooks: A Practical Guide
Favicon
Building Powerful React Components with Custom Hooks
Favicon
React Custom Hooks (useNetworkStatus)
Favicon
React Custom Hooks (useMediaPermission)
Favicon
React Custom Hooks (useDebounce)
Favicon
How to Create a Counter App with useReducer and Custom Hooks
Favicon
How to create custom Hooks in React.Js?
Favicon
React Slideshow Component with autoplay, fullscreen mode and expand all
Favicon
React Portals: create and open modals with keyboard keys
Favicon
Learn Something on React JSX and Hooks
Favicon
How to create Tabs in ReactJs using Hooks ?
Favicon
Simple hook to handle featue flags
Favicon
React custom hooks to update form data
Favicon
useIpAdrress()
Favicon
Custom hook useScroll.tsx :: React TypeScript
Favicon
useQuery() with React Router Dom
Favicon
React Custom Hooks
Favicon
useGeoPosition Hook - A Custom React Hook to grab latitude and longitude from a given address.
Favicon
`useBackButton` hook to handle back button behavior in React Native
Favicon
Form in Modal using React hooks – mistakes and lesson learnt

Featured ones: