DEV Community

Cover image for React JS - Naming convention
Kristiyan Velkov
Kristiyan Velkov

Posted on • Originally published at Medium

React JS - Naming convention

In computer programming, a naming convention is a set of rules for choosing the character sequence to be used for identifiers that denote variables, types, functions, and other entities in source code and documentation. (source: Wikipedia)

When it comes to naming conventions in React.js, consistency and clarity are key.

A well-defined naming convention helps improve code readability, maintainability, and collaboration.


Naming Convention

 

Components

Use descriptive and meaningful names for React components. Use PascalCase (capitalizing the first letter of each word) for component names.

Image description


Files

Name your files using PascalCase, matching the component name. For example, if you have a component named UserCard, the file should be named UserCard.js.

Image description


Props

Use descriptive names for props to clearly indicate their purpose. Avoid abbreviations or acronyms unless they are widely understood in the context of your project.

For example:

  • instead of usr, use user.

Image description

In this example, the prop user is used to pass user data to the UserCard component. By using a descriptive name like user, it's clear that the prop represents user data, making the code more readable and understandable.


State variables

Prefix state variables with is, has, or should to denote boolean values.

Image description

_In this example, we have three state variables: _

  • isActive,
  • hasError
  • shouldRender

These variables are prefixed with is, has, and should, respectively, according to the naming convention.

The corresponding state update functions setIsActive, setHasError, and setShouldRender are used to modify the state.

By following the naming convention, the purpose and meaning of these state variables are clear, making the code more readable and maintainable.


Event handlers

Use handle as a prefix for event handler functions. For example, handleClick, handleInputChange.

Image description


CSS classes

Use lowercase letters and hyphens for CSS class names.

Image description


Constants

Use uppercase letters with underscores to represent constants in JavaScript.

For example, API_URL, MAX_RESULTS.

Image description


Utility functions

Choose descriptive names that indicate their purpose or functionality.

For example, formatDate, generateUniqueId.

Image description


Remember, the most important aspect of naming conventions is consistency within your project or team. It's also a good idea to document your naming conventions so that all team members can follow them consistently.


Image description

linkedin


Image description

If you like my work, please:

1. Donate 💕💸

Revolut website payment or use the QR code above.

Your donation will fuel my drive to continue creating meaningful work. Thank you! 🦁💚

2. Subscribe 🤗


Top comments (2)

Collapse
 
moopet profile image
Ben Sinclair

Just a heads up that the Markdown we use here supports syntax highlighting, and is generally more accessible than inserting an image of code. Images of text are an issue for people using screen readers, for example, and their content won't get picked up by the site's search facility.

You can add code blocks with 3 backticks: code block with colors example More details in our editor guide!

Collapse
 
kristiyan_velkov profile image
Kristiyan Velkov

Thank you for letting me know. I will use Markdown for code snippets to improve accessibility and searchability.