Blog>
Snippets

Feature Flag-Driven Conditional Rendering

Provide an example of a React component that uses Redux state selectors to conditionally render elements based on the state of a feature flag.
import { useSelector } from 'react-redux';

// Selectors
const selectFeatureFlagState = (state) => state.featureFlags;

// React component that conditionally renders based on feature flag state
const FeatureFlaggedComponent = () => {
  // Using useSelector to get the feature flag state
  const featureFlags = useSelector(selectFeatureFlagState);

  // Conditional rendering based on feature flag
  return (
    <div>
      {featureFlags.newFeatureActive ? (
        <div>This is the new feature that is currently turned on.</div>
      ) : (
        <div>This feature is not yet available.</div>
      )}
    </div>
  );
};

export default FeatureFlaggedComponent;
This code defines a React component that uses Redux's useSelector hook to access the feature flag state from the Redux store. It then conditionally renders content based on the state of the 'newFeatureActive' feature flag.