React JS - Naming convention

kristiyan_velkov

Kristiyan Velkov

Posted on June 18, 2023

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 🤗


💖 💪 🙅 🚩
kristiyan_velkov
Kristiyan Velkov

Posted on June 18, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related