Building a React Dashboard
@xav-b|November 26, 2015 (9y ago)11 views
I spent the last six months working on data analytics and machine learning to feed my curiosity and prepare my new job. It is a challenging mission and I chose to give up for a while on my current web projects to stay focus. Back then, I was coding a dashboard for an automated trading system, powered by an exciting new framework from Facebook : React. In my opinion, Web Components was the way to go and React seemed gentler with my brain than, say, Polymer. One just needed to carefully design components boundaries, properties and states and bam, you got a reusable piece of web to plug anywhere. Beautiful.
This is quite a naive way to put it of course but, for an MVP, it actually kind of worked. Fast forward to last week, I was needing a new dashboard to monitor various metrics from my shiny new infrastructure. Specialize requirements kept me away from a full-fledged solution like InfluxDB and Grafana combo, so I naturally starred at my old code.
Well, it turned out I did not reuse a single line of code. Since the last time I spent in web development, new tools, frameworks and methodologies had taken over the world : es6 (and transpilers), isomorphic applications, one-way data flow, hot reloading, module bundler, ...
Even starter kits are remarkably complex (at least for me) and I got overwhelmed. But those new toys are also truly empowering and I persevered. In this post, we will learn to leverage them, build the simplest dashboard possible and pave the way toward modern, real-time metrics monitoring.
Tooling & Motivations
I think the points of so much tooling are productivity and complexity management.
New single page applications usually involve a significant number of moving parts : front and backend development, data management, scaling, appealing UX, ... Isomorphic webapps with nodejs and es6 try to harmonize this workflow sharing one readable language across the stack. Node already sells the "javascript everywhere" argument but here, it goes even further, with code that can be executed both on the server and in the browser, indifferently. Team work and reusability are improved, as well as SEO (Search Engine optimization) when rendering HTML on server-side.
Yet, applications' codebase can turn into a massive mess and that's where Web Components come handy. Providing clear contracts between modules, a developer is able to focus on subpart of the UI with an explicit definition of its parameters and states. This level of abstraction makes the application much more easy to navigate, maintain and reuse. Working with React gives a sense of clarity with components as Javascript objects. Lifecycle and behavior are explicitly detailed by pre-defined hooks, while properties and states are distinct attributes.
We still need to glue all of those components and their dependencies together.
That's where npm, Webpack and Gulp join the party. Npm is the
de facto package manager for nodejs, and more and more for frontend
development. What's more, it can run for you scripts and spare you from
using a task runner like Gulp. Webpack, meanwhile, bundles pretty much anything
thanks to its loaders. Feed it an entrypoint which require
your js, jsx,
css, whatever ... and it will transform and package them for the browser.
Given the steep learning curve of modern full-stack development, I hope you can see the mean of those tools. Last pieces I would like to introduce for our little project are metrics-graphics and react-sparklines (that I won't actually describe but worth noting for our purpose). Both are neat frameworks to visualize data and play nicely with React, as we are going to see now.
Graph Component
When building components-based interfaces, first things to define are what
subpart of the UI those components are. Since we start a spartiate
implementation, we are only going to define a Graph
.
This code, a trendy combination of es6 and jsx, defines in the DOM a standalone
graph from the json data in confidence_band.json
I stole on Mozilla
official examples.
Now let's actually mount and render the DOM in the main entrypoint of the application (I mentioned above with Webpack).
Now that we defined in plain javascript the web page, it's time for our tools to take over and actually build it.
Build workflow
This is mostly a matter of configuration. First, create the following structure.
Where package.json
is defined like below.
A quick npm install
will download every package we need for development and
production. Two scripts are even defined to build a static version of the site,
or serve a dynamic one that will be updated on file changes detection. This
formidable feature becomes essential once tasted. But we have yet to configure
Webpack to enjoy it.
Webpack configuration can be hard to swallow at first but, given the huge amount
of transformations to operate, this style scales very well. Plus, once setup,
the development environment becomes remarkably productive. To convince yourself,
run webpack-dev-server and reach localhost:8080/assets/bundle.js
in your
browser. Tweak the title
argument in main.jsx
, save the file and watch the
browser update itself. We are ready to build new components and extend our
modular dashboard.
Conclusion
We condensed in a few paragraphs a lot of what makes the current web ecosystem effervescent. I strongly encourage the reader to deepen its knowledge on those matters and consider this post as it is : an introduction.
Web components, like micro-services, are fun, powerful and bleeding edges. But also complex, fast-moving and unstable. The tooling, especially, is impressive. Spend a hard time to master them and craft something cool !