Let’s face it: developing scalable front-end code isn’t piece of cake. No matter how well-structured the framework/architecture you’re using is, everything will be converted to ye olde HTML, CSS and (vanilla) JS.
Well, the good news is the open-source community already created solid frameworks (like AngularJS and ReactJS) to make your life incredibly easier, so you can work on high-level code and nevermind the hardcore stuff.
This brings up a new scenario, though – after getting used to building things the old-fashioned way (or not-so-old-fashioned, with tools like Backbone.js or Ember.js), you’ve decided to try what is trending on front-end development now: ReactJS, using the Redux architecture. You got excited with the possibilities this opens (and – oh my! – the ability to ditch jQuery once and for all), and, after working for a few weeks you realize that your code is a total mess.
Don’t be alarmed – you can fix that. I’ll share a few insights on how I transformed my Frankenstein into a clean and Redux-compliant codebase, while still being able to deliver new features.
The insights from this article might also be useful for seasoned developers, who want some tips on how to achieve a better-organized and scalable code.
There are two types of components in the React/Redux architecture: presentational and container components.
We could split it into the following components and containers:
You can observe that it is possible (and fairly common) to put a container inside a container, in order to modularize and structure the code.
1. Define the role of your components
When starting with React+Redux, people tend to build massive components, which contain a whole bunch of logic and connect to many Store states. It is not uncommon to find people treating components as HTML templates — you have one for each page, and you simply translate the HTML code into JSX in therender() function.
Developing things in such way invalidates one important advantage of this pattern, which is the ability to reuse components throughout your web application, and propagate state changes when they affect more than one component or page.
There are two types of components in the React/Redux architecture: presentational and container components.
- Presentational Components (often referred to as ‘dumb components’) are all-normal ReactJS components, which do not contain any logic or store connection – they are simply ‘views’ to display data. These components usually receive some
propsand display them or trigger actions depending on the user behavior (the actions should be sent aspropsto the components, as no business logic is defined on presentational components). - Container Components (often referred to as ‘smart components’), on the other hand, don’t do anything visual: they are the ones who’ll process and pass data to the presentational components. Containers will be the components that connect to the Store and import actions, so you’ll often see
mapDispatchToProps()andmapStateToProps()definitions inside them.
1.1. Break your containers into presentational components and container components
This will enable you to create several pieces of reusable code, so that your front-end will have a consistent look, and you’ll force yourself to avoid duplicated code to address similar problems. Taking this blog’s index as a reference:
We could split it into the following components and containers:
Suggestion of componentization of this blog’s index. Notice that this is for illustration purposes, as it’s up to the developer to define the granularity of each visual component.