Modern JS with React

by Andrew H Farmer


Keep up with modern JavaScript: React, Webpack, and Babel.

React AJAX Best Practices

February 05, 2016

When you start asking about AJAX and React, the first thing the experts will tell you is that React is a view library and React has no networking/AJAX features.

It's good to know, but not very helpful when you just want to get data from the server into your React components.

The truth is, there are a ton of ways to do it. You probably have thought of a couple ways yourself, but if you choose the wrong way, your code could get messy.

AJAX/HTTP Library Comparison

February 04, 2016

Networking in JavaScript falls into 2 categories: AJAX (browser) and HTTP clients (server).

Sometimes you'll only need one of these, sometimes you'll want both (in an isomorphic/universal app for instance).

Either way, you'll probably want a nice concise syntax. Most developers find the XMLHttpRequest API a little too verbose for their taste.

How AJAX works in React

February 03, 2016

React can be used with static data - but this gets boring preeeettty quickly. You might be left wondering:

How do I populate my React components with data from a server-side API?

The truth is, React is a view library and doesn't provide any networking capabilities. If this is news to you, go read about what react actually is, then head back here.

Top 5 Tutorials for Getting Started with React

January 15, 2016

Recently I found a developer asking this question about React:

"What tutorial would you recommend to get started? I'm looking for something super simple."

What is React?

January 02, 2016

It's the 🎊 new year 🎉 and it's a great time to learn some new skills! React is surging in popularity and for good reason: it is extremely useful.

But when developers consider learning React, they often come up against a problem: they aren't sure what it is.

You've probably read multiple 'what is react' descriptions at this point, but you might have found them a bit lacking. Here are some that I found:

Find your perfect React starter project

December 06, 2015

Looking for a React starter project? If you are, you probably had no problem finding one. In fact, you probably found TONS of them.

The boilerplate projects that pop out first have lots of github stars - AND lots of packages/dependencies. That can be overwhelming.

Here's what I hear from developers:

8 no-Flux strategies for React component communication

November 21, 2015

In React, one of the first big issues that comes up is figuring out how components should communicate with each other.

What's the best way to tell Component1 that Component2 was clicked?

If you start to dig a little, you'll get a ton of answers. Sooner or later Flux will be mentioned, which will only raise new questions.

How to validate nested proptypes with React

November 20, 2015

So you're writing a React component. You know how to pass props to your component, and you know how to reference the props in the component with this.props.propname.

But there's something in the middle that's a little tricker: propTypes.

In this article I'll answer questions that fit this formula:

Webpack HMR Tutorial

November 19, 2015

Struggling to set up HMR with Webpack?

Are you finding the Webpack documentation to be lacking - particularly in the HMR section? Does it seem like they've left out some important parts?

If so, read on! We'll fill in the gaps together.

Guide to JavaScript frontend package managers

November 14, 2015

When I started my last frontend JavaScript project I wasn't super familiar with all the package managers, what they do, and what they're for, so I quickly became very confused.

I was recently reminded of that time when I saw this question:

Are there good reasons for NOT linting your code?

November 06, 2015

Code linters are 'metaprograms'. They are programs that take your code as input, and as output give you notes on how to improve it.

I have found linters to be invaluable so I always use one when I code. Lately I've been thinking...

3 Ways to Set Up Webpack with HMR

November 06, 2015

It's not clear in the Webpack documentation, but there are exactly 3 ways that you can set up Webpack's hot module replacement (HMR) feature.

Let's jump right into the 3 ways. These are ordered from the simplest setup to the most complex, but none of them are that complicated. The hardest part is picking which approach is right for you.

Run the webpack-dev-server on the command-line. With this approach you often don't need to change your webpack.config.js file, so it's the simplest. WDS CLI Docs.

Understanding Webpack HMR

November 05, 2015

Hot Module Replacement, or HMR, is essential for the fast and efficient client-side Javascript developer. I know this, and yet recently found myself not using it in my recent projects. Why?

I didn't understand how HMR works. Sometimes my projects didn't exactly match the provided examples or starter projects - and then I became lost.

Read on to get a deep understanding of what HMR is and how it works so you can use it in all your projects.

Do more in less time with Webpack HMR

November 04, 2015

Hot Module Replacement is a Webpack feature that updates your Javascript without a browser reload. It can even maintain your application state across code updates. It's primarily used for development.

This article is about why you should be using it - so if you've never heard of HMR, or if you don't use it for all your projects, read on.

Let's take a step back and talk about your development cycle. I'm not talking about project management or agile. Think smaller. I'm talking about this cycle: type code -> reload your app -> type more code -> ....

Simple image gallery react component

November 03, 2015

It turns out it's really easy to build an image gallery with a loading indicator (spinner) in React. This one is a single component with only 70 lines including spacing and comments.

I'll walk you through creating the gallery component piece by piece. Here's what you'll get when you're done:

The refresh button is not really part of the component - I added that so you can see the spinner in action.

Detect when images have finished loading with React

November 02, 2015

I was asked this question recently:

"Is there a way to detect when the children of a component have rendered completely?"

And the answer is, of course, componentDidMount(). componentDidMount() is called when your React component is rendered.

Discord: Hide the Clunky Sidebar

October 24, 2015

Discord is a great voice/text app for gamers. I'm not much of a gamer, but since Discord began hosting the Reactiflux chat room, I have become a frequent user and a big fan.

Discord still has some quirks though. One is that the sidebar is huge and there's no way to hide it.

Luckily, Discord is a web app, and they've exposed the Toggle Developer Tools feature. So we can hack it like a web page!

Why you need to use refs instead of IDs

October 21, 2015

Let's start by assuming that you went ahead and used an ID to access one of your elements in a React component. What could happen? Maybe your code looks like this:

class IdComponent extends React.Component {
  onFocus() {
    document.getElementById('myInput').setAttribute('class', 'highlight');
  }
 
  onBlur() {
    document.getElementById('myInput').setAttribute('class', '');
  }
 
  render() {
    return (
      <div>
        <input id="myInput" onFocus={this.onFocus.bind(this)} onBlur={this.onBlur.bind(this)}/>
      </div>
    );
  }
}

This would actually work. At first. Here's a demo. Tap the input field to see the highlight:

Use aws-sdk with Webpack

October 19, 2015

If you are using webpack and you thought you could just import AWS from 'aws-sdk' or require('aws-sdk')... you are out of luck. You'll see one of these errors:

Module not found: Error: Cannot resolve module 'fs' in ...

the request of a dependency is an expression

How Whitespace Works in JSX

October 03, 2015

Familiar with how whitespace works in HTML, but not getting the results you would expect in your React component? Read on.

The easiest way to learn is probably by example, so here we go, example city!

In these examples, HTML and JSX agree on whitespace. Not super interesting - so just scan it and let's move on. For clarity, denotes a single space.

How To Pass Events Between Components

October 02, 2015

Imagine you have a component with a render() method like this:

render() {
  return (
    <div>
      <A/>
      <B/>
    </div>
  );
}

If <A/> has an <input> inside of it, and you want to capture any changes to that input, how would you do it? Turns out it's really easy. The render() method that does it looks like this:

Event Bubbling

September 20, 2015

Have you ever been unsure of where to put your onChange or other event handler in React? Does it go in the parent, or the child? Can you put a handler in both places? Should you pass event handlers with props?

If you've ever asked yourself any of those questions, this article is for you.

Let's start by talking about how event bubbling works without React, in the regular DOM.

Webpack Watch in Vagrant/Docker

August 27, 2015

So you just got Webpack all set up, and you're using Vagrant or Docker. Then you run webpack --watch. But when you make changes in your favorite text editor, Webpack doesn't even notice! It does nothing! Why?

First let's discuss how this normally works on Linux:

  • You run webpack --watch, which subscribes to file changes using inotify.
  • You make changes in your text editor.
  • inotify picks up the changes and reports them to Webpack.
  • Webpack does a rebuild, yay!

Meta Post

August 23, 2015

I just launched a new blog. And this is it. You're reading it right now.

This new blog is built on gulp, bootstrap, and metalsmith. :)