Over the past two months, I have been working on an interesting project at my company which is supposed to be a very big one. So at first, as a team leader and core maker, I spent quite a lot of time to find a good starting point, I meant stuff like how to structure the project, which frontend stack to use, how that stack connects to server, how to set up and bring all those things together, etc, to satisfy all the project’s requirements.
Finally, I settled down with Meteor for backend, React for frontend, and Vanilajs ES6 MVC for the app extension (this was the most interesting part of the project. I will talk about it in another blog). At first things went well and smooth. Needless to say that React was so amazing that I completely changed my thoughts when programming.
But soon later, since this was the first time I had really built such a big project with React, things started to get messy. I encountered a problem that every big project coded in React had. Hence, I have this post written to let you know what problem was and how I attempted to solve it.
React is introduced to be simple and very easy to learn for beginners. That is definitely true, but frankly speaking, it takes quite an effort to come up with when you are at beginner level.
Unfortunately, this post is not aimed for beginners because I am writing about experience I learned in a big project, not just the way to use React for Meteor project. So it may require readers to have the following knowledge to understand what I am going to write:
The problem that I encountered
I can summary the problem with only one word, and that is maintainable.
Follow the unidirectional data flow principle, I created a container component for each routes for the problem. This component is in charge of almost every logic stuff for the route it is being used for. E.g. it takes care of subscribe data/call methods from Meteor server, is a middle man for its children components talking to one another. With all those missions, soon it becomes a very big and fat guy.
The file in which I coded the container component in was about 700 lines when I just finished about half of the work. This was clearly unacceptable. If I had ignored this problem and kept coding to get the job done fast, it would have been a nightmare for maintainer/debugger.
Knowing that this was my company’s main project, meaning that my colleagues and I would be working on it for years, I stopped coding features and started reviewing the code.
I looked at all the container’s functions and realized that they could be categorized into four groups:
- Component’s must-have functions: getInitialState, getMeteorData, render, etc…
- Handler functions: all event handlers for all container’s children components
- Helper functions: functions to help get necessary data for container’s children components based on container props and state. There functions are merely pure functions, they take input and produce output without touching the container state or talking to Meteor server.
- Logic functions: functions to modify the component state, call methods from server. These functions are called by event handlers in response to user actions.
With this model in mind, I decoupled the last three groups’ functions into three separate files and imported them back in the container through mixins.
The directory of the container looked like this:
And also the code of the container