5 common mistakes to avoid when using React in 2022

Since Facebook released React in 2013, it has become one of the most widely used JavaScript frameworks. According to Statista, React is the world’s second most used web development framework. As the popularity of JavaScript remains high, React utilizes its capabilities to provide the most comprehensive tool sets to build web and mobile applications.

As a React developer, you have the opportunity to be part of a technology that has immense growth potential shortly. More and more web developers are adopting this JavaScript framework. Backed by Facebook and a vast developer community, React is a framework to master if you want to become a web developer.

However, there are some common mistakes that you must avoid. Here we take a look at the most common React mistakes developers commit.

1. Not creating enough components

A common mistake any React developer can make is not creating enough components. React is a highly versatile language, and if you are creating a few significant components, you’re missing its reusability. While it is not wrong to produce large components that execute many tasks, it is recommended that you create smaller components, more preferably, one component corresponding to one function. This approach saves time and is a significant benefit when debugging the code. Any errors can be easily spotted as you know which components are associated with which functions.

Here is an example of a TodoList component broken down to single functions:

// ./components/TodoList.js

 import React from ‘react’;

 import { useTodoList } from ‘../hooks/useTodoList’;

import { useQuery } from ‘../hooks/useQuery’;

import TodoItem from ‘./TodoItem’;

import NewTodo from ‘./NewTodo’;

const TodoList = () => {

  const { getQuery, setQuery } = useQuery();

  const todos = useTodoList();

  return (

   <div>

    <ul>

     {todos.map(({ id, title, completed }) => (

      <TodoItem key={id} id={id} title={title} completed={completed} />

     ))}

     <NewTodo />

    </ul>

    <div>

     Highlight Query for incomplete items:

     <input value={getQuery()} onChange={e => setQuery(e.target.value)} />

    </div>

   </div>

  );

};

 export default TodoList;

2. Modifying the state directly

Another common mistake React developers commit is modifying the state directly. As a rule of thumb, in React, the state must always be immutable; otherwise, there will be performance issues that will be difficult to fix.

Here’s a code:

const modifyPetsList = (element, id) => {

  petsList[id].checked = element.target.checked;

setPetsList(petsList);

};

Here we want to update the checked key of an object in the array based on the state of the checkbox, but there is an issue. React cannot observe and trigger the re-rendering of the object because it has been changed with the same reference.

Either you can use the setState() method or the useState() hook to fix the issue. These methods will ensure that React acknowledges the changes made to the object and that your DOM is correctly re-rendered.

3. When rendering the list, do not use the key

If you are a beginner or used our React developer toolkit to learn the language, you must have come across the prompt when you render a list according to the method described in the documentation.

For example, rendering these arrays:

const numbers = [1, 2, 3, 4, 5];

const listItems = numbers.map((number) => <li>{number}</li>);

will display this prompt on the console “a key should be provided for list items.”

The solution is obvious here. We have to follow the prompts and add the key attribute to each item like this:

const numbers = [1, 2, 3, 4, 5];

const listItems = numbers.map((number, index) => <li key={index}>{number}</li>);

The key helps React identify which elements have changed, which is why you need to assign a unique value to the key. In the above example, we have used the index as the key value.

However, we do not recommend you to use the key and ignore the prompt.

This is because the value of the key will change every time an item is added or removed from the array. It will result in performance degradation.

4. Using Redux too much

Redux is becoming very popular among React developers, primarily working on large apps. It is helpful as it helps manage global state, but you don’t have to use it to manage every state in your React apps.

If your applications do not use parallel-level components that need to exchange information, then there is no need to use Redux. Instead, you should use a local state method or useState when you use form components or want to check the state of an element every time it is accessed.

5. Incorrect use of boolean operators

In JSX/TSX syntax, React developers often use boolean values to control rendered elements utilizing the && operator like this:

const count = 0;

const Comp = () => count && <h1>Chris1993</h1>;

While we want that page to display empty content at this time, it will actually display 0 on it.

The error is because the falsy expression causes elements after && to be skipped. However, the value of the falsy expression is returned.

The correct way to write the condition without relying on the JavaScript’s boolean value to compare:

const count = 0;

const Comp = () => count > 0 && <h1>Chris1993</h1>;

Now the page will display empty content.

Conclusion 

Now that you have learned some common React mistakes, keep them into account when creating your next React app. Gradually you can inculcate these best practices and soon they will become a learned behavior and improve your code quality.

And if you are an experienced React developer, join Talent500. We are a global remote team-building platform that Fortune500 companies and fast-growing startups use to hire talent.

 

Top 4 mistakes you should avoid as a junior developer

Whether you are beginning your career or switching fields, starting at a junior level is usually daunting. You expectedly know less than your peers and are bound to make mistakes. Junior developers are not expected to know everything, but there are some common developer mistakes they must avoid. When you start, you must keep a learner’s outlook, try to learn, and prevent imposter syndrome. Rather than faking it, try to learn through your mistakes.

In this article, we are listing the most common mistakes that junior developers tend to make and how these developer mistakes can be avoided.

1. Googling rather than reading documentation

Indeed, Google has made it a lot easier for developers to find solutions when they are stuck. It is quicker to Google the answer rather than reading the official documentation. We understand it’s boring to go through the documentation, and it’s not as fast as Googling the solution.

However, as a beginner, you are depriving yourself of learning the broader side of development by depending on Google. Documentation is one of the best ways to learn about several user cases and possible bugs.

Contrary to what most junior developers believe, reading docs provide better insight. It saves you a lot of time that would be otherwise wasted on Googling dozens of answers. 

When you read the docs, you know what to search in Google. Furthermore, referencing documentation is a great habit that helps you become a better developer.

When you start a new project or join one in the middle of the development lifecycle, you will most likely depend on the Readme documentation to understand the requirements and scope of the project.

2. Not asking for help when needed

One of the most common problems a junior developer faces is not knowing when to ask for help. You might start working on a project and decide to deliver results on your own without taking assistance from peers. As a beginner, you will get stuck with a problem, and after a few tries, you will find the solution, but it might not always be the case. Some issues will be too hard to solve on your own, no matter how much time you spend trying to solve them.

While repeatedly trying to solve a problem is part of learning, you need to know when you need help. There will be issues beyond your knowledge’s scope and require clarification. You cannot waste too much time on a single problem as your team needs to move forward with the project. 

When the deadline is looming, or you have tried everything in your capability and are still stuck with the problem, be open to help. Seek help from your peers and senior developers and ask them to explain the solution to you.

3. No risks, only comfort

Not only for developers, but comfort is a growth-kill for any profession. Junior developers pick easy and comfortable tasks and enter a comfort zone that hinders their growth. You get a false sense of accomplishment when you only stick with the easy development tasks. It will put you in the habit of not challenging yourself to take responsibility for your growth.

Junior developers who do not challenge themselves for growth and get complacent in their comfort zone hardly move up the career ladder. If you do not want to be stuck at the same level, it is necessary to punch above your weight. Proactively ask for challenging tasks but not all the time. You must balance easy and challenging tasks to avoid your comfort zone and not get overwhelmed by the challenges too often.

4. Stop watching tutorial videos

Tutorials are great for learning new technologies, but watching tutorial videos and not practicing actual code will not do any good. While tutorial videos are fun to watch, a reason why many junior programmers try to learn through video tutorials but they forget that it’s a costly venture. 

When you stick with video tutorials, you are trading time to watch those videos. You will have much less time to code and learn by doing.

When you code yourself, your progress is much more accelerated. Junior developers should start a project and ‘learn with a purpose.’ There is no point in randomly watching tutorials if you are never going to use what you see. 

This is why it is essential to use the information you learn from the tutorials by implementing it in your code. When you work on a project, you have a reason to search for tutorials and implement what you learn immediately.

Apart from nurturing technical skills, junior developers must also focus on essential soft skills.

Conclusion 

As a junior developer, you are prone to make these mistakes. We hope you understand what causes these mistakes and what can be done to avoid them.

Talent500 is a platform for companies to build their remote development teams. Join our elite pool of talent to be discovered by the best companies. Sign up here.