Checks whether strings match React naming conventions. π
Lint rules and other static analysis tools often need to know whether a name refers to a React component or hook. React's own tooling checks this internally, but doesn't export those checks for others to use. As a result, many projects copy and paste their own slightly different regular expressions.
This package gives you one small, dependency-free implementation that matches how JSX and React treat names today.
npm i is-react-nameimport { isComponentName, isHookName, isReactName } from "is-react-name";
isComponentName("MyComponent"); // true
isComponentName("myComponent"); // false
isHookName("useState"); // true
isHookName("useless"); // false
isReactName("MyComponent"); // true
isReactName("useState"); // true
isReactName("myFunction"); // falseAll functions return false for strings that aren't valid JavaScript identifiers, such as "My-Component" or "use State".
Whether a string is a valid React component name: it starts with an ASCII uppercase letter or any non-ASCII character.
This matches JSX, which only treats ASCII-lowercase tag names such as div as DOM elements.
isComponentName("App"); // true
isComponentName("εζ± θ§εΎ"); // true
isComponentName("app"); // false
isComponentName("_App"); // falseWhether a string is a valid React hook name: it's use itself, or use followed by an ASCII uppercase letter or digit.
This matches eslint-plugin-react-hooks and the React Compiler.
isHookName("use"); // true
isHookName("use3D"); // true
isHookName("user"); // false
isHookName("use_state"); // falseWhether a string is either a valid React component name or a valid React hook name.
isReactName("App"); // true
isReactName("useState"); // true
isReactName("app"); // false
isReactName("useless"); // falseSee .github/CONTRIBUTING.md, then .github/DEVELOPMENT.md.
Thanks! π
Josh Goldberg β¨ π» π π π€ π π§ π π§ |
π This package was templated with
create-typescript-appusing the Bingo framework.