Skip to content

Error when using useFeature hook and feature is not defined #734

@Tiberriver256

Description

@Tiberriver256

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

Metadata

Metadata

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions