Aaron

Graphic Design Portfolio

Coding an Advent Calendar: Day 7 (REST API + ReactJS)

This year, I decided to create an Advent Calendar website in HTML5, CSS3, and JavaScript. My hopes for the site are to showcase some of my frontend development abilities as well as make something fun, functional, and reusable.

Day 7

WordPress 4.7 “Vaughan”

WP REST API!!
Today is the big day! WordPress 4.7 launched this morning (in Korea) with the WordPress REST API content endpoints included in core (and a pretty great updated REST API Developer Reference as well). So… you know what that means. Today is the day I get the Advent Calendar to pull my blog posts with the (core) WP REST API! There are X steps:

  1. Revisit Day 2 where I set up the basic infrastructure for ReactJS & REST
  2. Update to WordPress 4.7 – where REST endpoints will work immediately (cool)
  3. Install Postman for Chrome* to test calls to the REST API
  4. Configure the REST URL (I have mine pull from THIS Category)
  5. Add it to the Codepen
  6. Watch the magic happen!! (…almost)

*Side note: Be sure to get Postman’s output as JSON for an easier view.

React Component
Actually, I hadn’t already created my React function to render the retrieved Component. I’d only filled in a blank <div> as a placeholder. So, the next step was to create an HTML structure in the React class that would receive the array of Posts from my REST API query and render them to the page.

Then, I added some basic CSS styling to make them look a little better on the page after loading.

BabelJS  
Finally, after setting everything up and Saving the Codepen, I noticed a small error in my JavaScript console in Chrome: "Unexpected token <". That’s weird, I thought. But then I noticed that the color highlighting in the Codepen was incorrect after a certain point – and the posts from React weren’t displaying quite properly. I checked back at the other Codepens I’d referenced that were using React to load JSON data and noticed that many were using the Babel JavaScript preprocessor. I thought, what the heck, added it to my project and *BAM!* the magic truly DID begin! Smooth loading of posts on the Codepen directly from this blog!

But… that’s Codepen. If I wanted to continue local development, it wasn’t going to be nearly so easy as that. I’d never heard of BabelJS before this, so I started investigating it and found out that it is often used with React and other scripts to create modern JS like ES2016. I realized I’d need to install the Node Modules for React, Babel, and Grunt to run all of these together, so my research and work began. There were X steps to complete this process:

  1. Run npm install --save-dev babel-cli babel-preset-latest babel-preset-react grunt-babel to install all the necessary development dependencies
  2. Run npm install --save react react-dom to save the necessary project dependencies
  3. Update package.json to recognize babel (rather than creating a .babelrc file)
  4. Update Gruntfile.js to “watch” for changes to my React app’s JS file and run the compile task
  5. Separate out my React Component code from the functions.js file to better manage the two separately
  6. Also, include React and React-DOM in the React app’s JS file

So far, I’ve been able to get Grunt to successfully “watch” for changes to my Sass files and my ReactJS files and run their compilation tasks. But, now I’m getting an Uncaught ReferenceError: require is not defined(...) error. Well, looks like fixing this is on the schedule for tomorrow!~

References

The following are some React and BabelJS references I was perusing today as I tried to get things to work. I haven’t finished reading through all of them yet:

  1. Official React site: A JavaScript Library for Building User Interfaces
  2. Writing React components as CommonJS modules
  3. NPM: A RequireJS plugin for loading jsx in require.js and r.js
  4. The React.js Way: Getting Started Tutorial
  5. 9 things every React.js beginner should know
  6. ES6 with Babel.JS
  7. Get To Know Babel The JavaScript Compiler Tutorial
  8. Setting up an ES6 Project Using Babel and Browserify
  9. Babel + Grunt === ES6 Now!
  10. ES6 with Babel & Grunt

Additionally, I just stumbled across a Lynda.com video that addresses using BabelJS to compile React. In this video, Eve Porcello shows how you can do in-browser Babel compilation by importing the script via a CDN, then using <script type="text/babel"> tags around any code you want compiled by Babel.

She does note that in-browser compilation is slow – and shouldn’t be used in a production environment. So, it’s good that I’ve started getting Babel set up as a module to precompile my JS before using it in the webpage.


Work Completed (to date)

  • December 7, 2016
    • Pull post data with the WP REST API in WordPress core!!
    • Write structural code for the React Component to be rendered
    • Install and setup BabelJS to compile the React code
  • December 6, 2016
    • Add README.md
    • Add a GitHub Issue to hold usable images
    • Add LICENSE
  • December 5, 2016
    • Create a GitHub repository and full site files for easier management
    • Setup Grunt.js to compile my Sass into CSS
    • Begin blogging about the process
  • December 4, 2016
    • Countdown clock (JS Date class & jQuery Easing) with SVGs
    • Dynamic text output for Year based on the current date
    • CSS only slider (off by 5px each slide)
  • December 3, 2016
    • CSS bow & ribbon
    • Footer with FontAwesome presents
    • Hover, active, and “Christmas Day” styles for footer presents
  • December 2, 2016
    • React.js + Axios.js initial code structure
    • Color palette
    • CSS → Sass
  • December 1, 2016
    • Let it snow
    • Typography choices
    • CSS presents (first design)
    • Design notes menu

Leave a Reply