Skip to content

About

Checks whether strings match React naming conventions. πŸ“›

Resources

Code of conduct

Contributing

Security policy

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

is-react-name

Checks whether strings match React naming conventions. πŸ“›

πŸ‘ͺ All Contributors: 1 🀝 Code of Conduct: Kept πŸ§ͺ Coverage πŸ“ License: MIT πŸ“¦ npm version πŸ’ͺ TypeScript: Strict

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.

Usage

npm i is-react-name
import { 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"); // false

All functions return false for strings that aren't valid JavaScript identifiers, such as "My-Component" or "use State".

isComponentName

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"); // false

isHookName

Whether 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"); // false

isReactName

Whether 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"); // false

Development

See .github/CONTRIBUTING.md, then .github/DEVELOPMENT.md. Thanks! πŸ“›

Contributors

Josh Goldberg ✨
Josh Goldberg ✨

πŸ’» πŸ–‹ πŸ“– πŸ€” πŸš‡ 🚧 πŸ“† πŸ”§

πŸ’ This package was templated with create-typescript-app using the Bingo framework.

About

Checks whether strings match React naming conventions. πŸ“›

Resources

Code of conduct

Contributing

Security policy

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages