Skip to content

Fix useFeature hook to warn instead of error when feature is undefined - #911

Draft
sergiodxa with Copilot wants to merge 5 commits into
mainfrom
copilot/fix-7ed6c250-34a8-4536-937a-043e9ce98f1f
Draft

Fix useFeature hook to warn instead of error when feature is undefined#911
sergiodxa with Copilot wants to merge 5 commits into
mainfrom
copilot/fix-7ed6c250-34a8-4536-937a-043e9ce98f1f

Conversation

Copilot AI commented Sep 27, 2025

Copy link
Copy Markdown

Fix useFeature hook to warn when feature is undefined

This addresses the issue where useFeature hook would throw an error when a feature is not defined. Instead, it should default to false and show a console warning.

Plan:

  • Understand the current codebase and existing tests
  • Add test case to verify expected behavior (console warning + default to false)
  • Modify useFeature hook to add console warning when feature is undefined
  • Ensure all existing tests still pass
  • Verify the fix handles all edge cases (simple features, nested features, arrays)
  • Run linting and build to ensure code quality
  • Remove package-lock.json since project uses Bun package manager

Changes:

  • Added console warning functionality to useFeature hook for three scenarios:
    1. When feature doesn't exist in array-style features
    2. When feature doesn't exist in object-style features (non-nested)
    3. When feature doesn't exist during nested feature traversal
  • Added comprehensive test cases covering:
    • Undefined simple feature with warning
    • Undefined nested feature with warning
    • Undefined feature in array with warning
    • Explicitly false features do not warn
    • Falsy but not undefined values do not warn
  • Removed package-lock.json and added to .gitignore since project uses Bun
  • All existing tests continue to pass

Current Status:

COMPLETE - The fix is fully implemented and tested. The useFeature hook now:

  • Returns false when a feature is not defined (instead of throwing an error)
  • Shows a helpful console warning: "Feature flag 'featureName' is not defined. Defaulting to false."
  • Works correctly for all feature types: arrays, simple objects, and nested objects
  • Maintains backward compatibility with existing functionality
  • Project properly configured for Bun package manager

The issue described in the problem statement is now resolved.

Original prompt

This section details on the original issue you should resolve

<issue_title>Error when using useFeature hook and feature is not defined</issue_title>
<issue_description>### Current Behavior

When I have code that looks like this:

import { useFeature } from "flagged";

export function Header() {
	const hasV2 = useFeature("v2");

	return <header>{hasV2 ? <h1>My App v2</h1> : <h1>My App v1</h1>}</header>;
}

createRoot(document.getElementById("root")!).render(
	<FlagsProvider features={{ v3: true, moderate: false }}>
		<Header />
	</FlagsProvider>,
);

We get an exception thrown from this split method

Expected Behavior

Would love for it to essentially default to the flag being off if it can't find the flag in the provider and then do something like a console.warn to let me know that the flag is missing rather than throwing a 'cannot split on undefined' error message.

Suggested Solution(s)

Check if the flag doesn't exit
If the flag requested by the hook doesn't exist, treat it as if the flag were disabled and then throw a warning into the console

Your Environment

Software Version
Flagged ^2.0.8
React 17
Browser Chrome
npm/yarn npm
</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #734

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@codesandbox-ci

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit fe9fc5f:

Sandbox Source
flagged-example Configuration

Copilot AI and others added 2 commits September 27, 2025 07:10
Co-authored-by: sergiodxa <1312018+sergiodxa@users.noreply.github.com>
Co-authored-by: sergiodxa <1312018+sergiodxa@users.noreply.github.com>
Copilot AI changed the title [WIP] Error when using useFeature hook and feature is not defined Fix useFeature hook to warn instead of error when feature is undefined Sep 27, 2025
Copilot AI requested a review from sergiodxa September 27, 2025 07:15
Comment thread src/index.tsx
Co-authored-by: sergiodxa <1312018+sergiodxa@users.noreply.github.com>
@sergiodxa

Copy link
Copy Markdown
Owner

@copilot remove the package-lock.json, this uses Bun as package manager

Co-authored-by: sergiodxa <1312018+sergiodxa@users.noreply.github.com>

Copilot AI commented Sep 27, 2025

Copy link
Copy Markdown
Author

@copilot remove the package-lock.json, this uses Bun as package manager

Removed package-lock.json and added it to .gitignore since the project uses Bun. The change is in commit f8a92c4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error when using useFeature hook and feature is not defined

2 participants