A few best practice for beginner react developer

Shamim AL-Mamun
4 min readMay 7, 2021

--

Today, we take the topic head on and show you the most fundamental of the React best practices. React is a JavaScript library which introduced in 2013. It’s a library for user interface or UI components design. Here are some of the best practices of React that a beginner should know before creating a react application.

1. Follow linting rules, break up lines that are too long

Linting is a process wherein we run a program that analyses code for potential errors.

Mostly, we use it for language-related issues. But it can also fix many other issues automatically, particularly code style. Using a linter in your React code helps to keep your code relatively error- and bug-free.

2. Use snippet libraries

Code snippets help you to keep up with the best and most recent syntax. They also help to keep your code relatively bug free, so this is one of the React best practices that you should not miss out on.

There are many snippet libraries that you can use, like, ES7 React, Redux, JS Snippets, etc.

3. Make your component reusable

Reusability is one of the core functionality of React that every developer should maintain properly. If you make a small component it will be easier and effective to reuse your component wherever you want. You don’t need to create the same component again and again. But in the case of a large component, you might not able to reuse your component because of its complexity

4. Use necessary comments

Use comments in your code when necessary for understanding and keep your code visually clutter-free. Other team members and developers could easily understand your code. It is also important for reusability also so that after a while you can easily understand and reuse your code whenever you want. But you should avoid unnecessary comment which will make your code complex and will create difficulties others to understand your code.

5. Code should execute as expected and be testable

Really, this rule needs no explanation. The code you write should behave as expected, and be testable easily and quickly. It’s a good practice to name your test files identical to the source files with a .test suffix. It’ll then be easy to find the test files.

You can use JEST to test your React code.

6. Write tests for all code

It is necessary to write tests for your code so that you can ensure that new code added to your application working perfectly with the existing code. As a decent practice, you ought to make a __Test__ directory inside your component’s directory to store every applicable test. You should test your components individually and also finally test your whole application. After the final test, your application will be ready to deploy.

7. Mind the other naming conventions

When working with React, you are generally using JSX (JavaScript Extension) files. Any component that you create for React should therefore be named in Pascal case, or upper camel case. This translates to names without spaces and the capitalizing the first letter of every word.

If you want to create a function that submits a form, you should name it SubmitForm in upper camel case, rather than submitForm, submit_form, or submit_form. Upper camel case is more commonly called Pascal case. Here is a further list of examples of variable and function names in Pascal case.

8. Name the component after its functionality

The naming of your component is very important to understand your application effectively and also for the purpose of reusability. After a while, you need to go through the whole code if you need to understand what functionality this component contains. If you name your component after the function or functionality you using on this particular component it will be easier and to reuse and also useful to the community.

9. Comment only where necessary

Attach comments to code only where necessary. This is not only in keeping with React best practices, it also serves two purposes at the same time:

It’ll keep code visually clutter free.

You’ll avoid a potential conflict between comment and code, if you happen to alter the code at some later point in time.

10. Build small components and function-specific

It’s not ideal to make components large and make your code more complex rather than one should try to keep components smaller so that they can be easily handled by the developer himself and other developers who might be work on this project later. Every component should correspond to one function. Then the application becomes easier to maintain and test.

11. Use capitals for naming components

Using an uppercase letter at the beginning of a component name is important so that JSX can differentiate between Html tags and your component name. For instance, you’ll name components as SelectButton instead of selectbutton, or Menu instead of menu. You can use lowercase letters but it will reduce the option of reusability of your code in your application and will confuse you.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Shamim AL-Mamun
Shamim AL-Mamun

Written by Shamim AL-Mamun

0 Followers

Dynamic and detail-oriented Web Developer with a skill for user-friendly, solutions effectively. I have an extensive understanding of Front-end and Back-end.

No responses yet

Write a response