Conditionally run different React hooks at runtime with a clean and safe API that follows the Rules of Hooks.
npm install hook-conditionalDynamic Hook Logic in React, Based on Any Runtime Condition Simplify conditional logic in your components by delegating it to purpose-built hooks mapped to values like roles, flags, environments, or status codes.




- π Switch on Any Condition β Support for strings, numbers, booleans, or any discriminant value.
- π§ Declarative Hook Selection β Map conditions to custom hooks and let React do the rest.
- β‘ Fully Compliant β All hooks are called unconditionally, following the Rules of Hooks.
- β¨ Tiny & Typed β Minimal, type-safe API with full IntelliSense support.
- π‘ Optional Fallback β Gracefully handle unmatched conditions with a fallback hook.
Install via your preferred package manager:
``shnpm
npm install hook-conditional
$3
`tsx
"use client";import { useConditionalHook } from "hook-conditional";
const Page = () => {
const env = process.env.NODE_ENV;
const value = useConditionalHook(env, {
development: () => useDevHook(),
production: () => useProdHook(),
test: () => useTestHook(),
});
return
{value}
;
};
`π API Reference
$3
Switches between custom hooks based on a condition. All mapped hooks are always executed to comply with Reactβs Rules of Hooks, but only the result of the matching hook is returned.
#### Parameters
-
condition (string | number | boolean)
The runtime value that determines which hook result to return.-
hookMap (Record)
An object where each key corresponds to a possible value of condition, and the value is a hook function to execute.-
fallbackHook (() => TResult, optional)
A fallback hook to use when no match is found in the map.#### Returns
-
TResult β The result of the matched hook (or fallback hook if no match is found).π Security & Provenance
This package is published with NPM Package Provenance, which provides cryptographic proof that the package was built from the source code in this repository using GitHub Actions.
$3
You can verify the package's provenance using:
`bash
Install the package
npm install hook-conditionalVerify the provenance
npm audit signaturesOr use the provided verification script
npm run verify-provenance
`$3
- β
Authentic Source: The package was built from this exact repository
- β
Secure Build: Built in a trusted GitHub Actions environment
- β
Tamper-Proof: Cryptographically signed to prevent supply chain attacks
- β
Transparent: You can verify the build process and source code
For more information about NPM Package Provenance, see the official documentation.
π‘ Examples
$3
`tsx
"use client";import useConditionalHook from "hook-conditional";
const Page = () => {
const role = useUserRole();
const permissions: string[] = useConditionalHook(role, {
guest: () => useGuestPermissions(),
user: () => useUserPermissions(),
admin: () => useAdminPermissions(),
});
return
Permissions: {permissions.join(", ")};
};
`$3
`tsx
"use client";import useConditionalHook from "hook-conditional";
const Page = () => {
const isEnabled = useFeatureFlag("new-ui");
const ui = useConditionalHook(isEnabled, {
true: () => useNewUI(),
false: () => useOldUI(),
});
return <>{ui}>;
};
`$3
`tsx
"use client";import useConditionalHook from "hook-conditional";
const Page = () => {
const status = useStatus();
const content = useConditionalHook(
status,
{
200: () => useSuccessContent(),
404: () => useNotFoundContent(),
500: () => useErrorContent(),
},
() => useDefaultContent()
);
return <>{content}>;
};
``Contributions are welcome! To contribute:
1. Fork the repository.
2. Create a feature branch.
3. Commit your changes.
4. Open a Pull Request.
Please ensure your code matches the project style and includes relevant tests if applicable.
This project is licensed under the MIT License - see the LICENSE file for details.
---
This package is developed and maintained by JP.Coffee. Feel free to reach out or open an issue for any questions or suggestions!