ReactJS anti-patterns you should avoid

ReactJS is a powerful library that can help you create component-based user interfaces. But, like any other library, it has its own set of anti-patterns that you should avoid when using React. In this article, we’ll look at some common mistakes in ReactJS and how to fix them!

Mixing logic with markup

Mixing logic with markup is a common React anti-pattern.

Mixing logic and markup causes the DOM to be hard to reason about and understand. That influences directly the performance and the ability to fulfill the code and commands you give to the browser. Keep them separate to avoid problems.

Inconsistent naming

The names of components, props, and state should be consistent. Consistent naming is important for readability and maintainability while also helping with code reuse.

If you have a component called in one file, but it is located in another one, it can make it hard to know what’s happening when you come back later on. You may even forget what the different components are called altogether! This makes it harder for new developers to pick up work on your codebase later on too!

Duplicate code

The main reason that you want to avoid duplicate code is that it makes your application harder to maintain. When you have duplicate logic, it’s easy for bugs to creep in and cause inconsistencies between two different parts of your application. If you refactor one part, you will need to make sure that all other instances in the project still work properly as well. It also just makes it more difficult for new developers on your team who aren’t familiar with the project yet — if they see two different pieces of functionality being handled by the same piece of code, they might not understand which one is newer or more important than another. In that way follow the DRY (don’t repeat yourself) principle and that will help you to avoid the same pieces of code and find solutions to keep your code simple and clear for understanding.

Having said that, there are times when duplicating something can be justified if done correctly: for example, if there are different variations on how an element should look depending on what state it’s in (like an error message), then having multiple templates would make sense! However, these cases should be few and far between; most commonly we find ourselves running into this issue when there isn’t a clear separation between our view layer and our business logic layer (which leads us to our next anti-pattern).

Overusing react lifecycle methods

You’ve probably heard the advice that lifecycle methods are a good idea to use sparingly. This is because they are an advanced feature, and coming up with a use case for them can be tricky. Often, they’re used when we could have achieved the same thing by writing less code and making better use of React’s component hierarchy.

One of the examples is implementing a custom component that needs to perform some initialization, such as learning about its parent component or rendering certain data based on props passed in by parents.

class CustomComponent extends Component {
componentDidMount() {
// Do something here
}
// Other methods here…
}

HOC vs Render props

  • HOCs are more powerful than render props.

The biggest difference between HOCs and render props is that you can use a higher-order component to add functionality to a specific type of component, whereas render props are only used to pass data down the component tree. In other words, you can use a HOC to wrap any kind of React element (including ones that were not originally designed for it), but an isolated “render prop” must be defined within its own file.

  • Render props are easier to understand than HOCs.

Because they let you use JSX syntax in place of function calls, render props are much easier for beginners to understand than higher-order components. However, this simplicity comes at the cost of flexibility and reusability — if your goal is simply passing data from one part of your app down through its nested structure without having any additional code run on those elements along the way (or if this behavior needs changing later on), then using render props might work better than writing out each step as an isolated function call like with traditional React components or higher order components!

Nested callbacks

Nested callbacks are a common situation in ReactJS code. They can be avoided easily by using async/await or Promise.all().

Async/await allows you to write asynchronous code that looks synchronous and is also easy to read. The syntax is very similar to writing synchronous code, except that you use the await keyword instead of return when calling an asynchronous function or method:

async function doSomething() {

return doSomeAjax();
}

async function doSomeAjax() {

//do some Ajax stuff here

} //this will execute immediately because it's not marked as async…

doSomething();

When you need a great developer or a dedicated team of developers that create your product, RIVO AGENCY can help you and provide a great specialist to close your needs.

Too many components in the root folder

  • You may have a deeply nested component tree. This is bad because it makes your app harder to reason about, and the React team doesn’t recommend it anymore. If you want to avoid this problem, use a single-file component or dynamic import() instead of multiple files.
  • Another option is using render functions instead of components: they’re simpler to understand and easier to debug when there are bugs in how they’re rendered.
  • Finally, you can use render props if you need an extra level of abstraction between parent and child components.

Logic in the render method is not a good idea!

This is probably the most common problem that people start to face as they grow their React applications. It’s also probably one of the easiest to fix, but it’s one of those things that can be really tempting for newcomers to do because you can see the result of your code in a browser so quickly, and rendering just works.

The problem is that logic shouldn’t go in your render method — it should be kept separate from view logic! In fact, I would say that having any logic at all in your render method is an anti-pattern!

To get around this issue, there are two main ways we could refactor our code: either we can use a function component instead (more on what these are later), or we could pass in some props as part of our render method which can contain the data needed for rendering (e.g., “shouldShowLoader” => true).

Not using a key attribute with iterable components

The key attribute is used to specify which elements are unique. It can be used on any element and it isn’t required for the root element or for components that have a key prop.

If you’re using an iterable component, such as, or (but not something like ), then you should use the key attribute to define how many times that component can be rendered in your application. This prevents React from rendering duplicate instances of these components when they are re-rendered.*

Concluding

ReactJS has a lot of pros and cons, just like any other library.

ReactJS is a great library. It has many pros and cons, just like any other library. ReactJS is not perfect, but it’s still the best tool out there for building web UIs.

So, you’ve learned some of the best practices to follow when working in React and what not to do. The best advice we can give you is don’t overthink it. Try experimenting with different approaches and see what works best for your project.

In case you need to developer an app or custom software for your business or personal need, RIVO AGENCY at your service. Drop us a line and we will figure our what exactly you need and create it for you according to your vision.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
RIVO AGENCY

RIVO AGENCY

94 Followers

Hi! We are a team of professionals with a proven expertise in delivering effective, secure and high quality digital solutions for a variety of industries