I’ve finally started to learn and play around with React, a Javascript library for building user interfaces developed by Facebook. And boy, it’s fun. After about 15 minutes of feeling icky about mixing JS and HTML, I started to love it — bringing together the functionality and markup when they’re so intertwined makes things so much easier to iterate on, debug, and understand.
I definitely still have TONS to learn, but I wanted to share some of the resources I found helpful, and the first thing I built with React.
I went through a lot of tutorials. Here were my favorites:
I made it a point to write out all the code from the tutorials (no copying and pasting!), which forced me to not skim over anything and reinforced the patterns I started to see. Here’s the repo where I kept everything.
Then, I took off the tutorial training wheels and built something small on my own. Let’s go through it. It’s a simple interface where users can type or paste in numbers and see summary statistics and a histogram of the data. And as the data changes, things should hopefully update accordingly.
It’s composed of 3 components: the input text box, the stats, and the histogram.
Below is the Main component, which contains the input box (and its initial value), handles when the user adds/updates text, and parses the text into numbers (which get passed to the Stats and Histogram components).
The Stats component is very simple. It takes in the parsed numbers from the input box, uses the Simple Statistics library to compute some descriptive stats (mean, median, etc.), and renders them as stat cards in a grid layout.
And finally, we have the Histogram component. Here’s where I mix in some D3.js. Both React and D3 are opinionated about how things should be rendered and updated. For this, I’m deferring most of that logic to D3 and hooking into React’s lifecycle methods of componentDidMount and componentDidUpdate to trigger when D3 should create the visualization and update it as the input data changes.
And that’s it. Here’s the demo of everything together. And here’s the repo with the components and example page. I used Webpack to package up the JS into one bundle for the example page. Webpack is awesome BTW, but I’ll save that for another post :) Until then, I can’t wait to learn and dig in more with React!