I have recently been on a Higher Order Component (HOC) craze. HOCs are a good tool for implementing cross-cutting concerns or common functionalities, such as logging and tracking. For more information on HOCs, check out this recent post by my colleague Mehdi Mollaverdi!
Then I discovered Functions as Child Components (FaCC) and a couple of my brain cells perished. FaCC’s are components that receive a function as their child. For example:
So let’s take a trip through struggle town.
After making our journey from Flux to Redux, we were happy with how clean and simple our state management had become so we kept ticking along.
A Little Bit of Background
During this project, we came across a few different use cases for some cross-cutting concerns, such as page load tracking, toggling new features on and off, and desktop/mobile toggling. We wanted to implement these in a generic and reusable way to avoid code duplication. For example, we had different pages (routes) in our application, and wanted to track user visits to those pages, but didn't want to duplicate this tracking code for every route.
We initially used React mixins for some of these problem, but ended up replacing it with higher-order components. In this blog post, I'll first provide a brief introduction to higher-order components (HOCs), and will then go through our journey for each use case and will explain each of the aforementioned techniques (mixins and higher-order component) in more details.
Hark! What is this Jest you speak of?
Think of it as several layers of improvement stuck on top of Jasmine. Some of the neat features Jest provides are:
- Automatically finds tests to run in your project
- Has in built support for fake DOM APIs, such as jsdom, that you can run from the command line
- You can test asynchronous code more easily using inbuilt mocked timer functions
- Tests are run in parallel so they go faster! Vroom vroom.
But the big drawcard is Jest’s automatic mocking of CommonJS dependencies using the require() function. Instead of specifying all the dependencies you want mocked, you do the opposite. For the subject under test, you just use jest.dontMock().
Co-authored: Daniel Stankevich
TL;DR: React is better if you use Flux…
Our Journey with React
Having recently decided to embrace React.js for some of our front end applications we soon reached the limitations of simply wiring components together. When you start with a simple React application you will create many components. At some point a change in one of these components will need to update another component. eg. If you change a Price text field, you need to change the page title to reflect that. Continue reading