Home > Designing, Others > I Learned How to be Productive in React in a Week and You Can, Too

I Learned How to be Productive in React in a Week and You Can, Too

February 8th, 2016 Leave a comment Go to comments

This article is not intended for seasoned React pros, but rather, those of us who make websites for a living and are curious how React can help us reason about updating user interfaces. I’ve been intrigued by React for some time, and now that it has gained some standing in the community as well as good reviews, the time to learn it seemed justified. There are so many new technologies constantly emerging in front end development that it’s sometimes hard to know if effort into learning something new will pay off. I’ll spend this article going over what I think some of the most valuable practical takeaways are so that you can get started.

Fair warning

I’ve only spent about a week working with this material. That’s on purpose. I’m writing this while I’m fresh with the technology and can write about it from that perspective. In this state I’m much better at remembering the tumbling blocks and findings along the way.

I used Wes Bos’ React for Beginners course, as well as React.js Introduction For People Who Know Just Enough jQuery To Get By. I could not recommend these resources more highly.

In two days, I made this:

See the Pen Baby’s First React Attempt by Sarah Drasner (@sdras) on CodePen.

It is imperfect, but it goes much farther than I would have gotten on my own in that amount of time.

If you’re the kind of person who wants to dive right in and learn it all, including the build and requisite tooling, that’s awesome! Here’s where to start. I would also suggest this pretty great Frontend Masters course: Modern Web Apps.

Mythbusters: Practicality in React Edition

Before we get started, there are a couple of key myths I’d like to debunk that I think are blockers for a lot of people.

Myth #1: You have to use inline styles to use React.

Nope! Not at all. You can use CSS just as you normally do. Having just spent a lot of time refactoring a giant CSS codebase, I would say that this is pretty important to establish. React, being fairly new, has not yet had to stand the test of design refreshes like other technologies have. If I had had to go back through thousands of lines of inline styles just to update things like padding and line-height, that would make me a sad developer indeed.

That said, there are times when inline styles do make sense. If you have a component that will change styles depending on its state (for instance: a data visualization) it would make absolute sense to work with inline styles so that you’re not maintaining an impractical number of static styles (in multiple places) to handle all possible states.

I’d think, though, that this would be on top of a base CSS that the application uses and be an exception rather than a rule. The web is a big place, though, so there are no absolutes.

Myth #2: You have to use JavaScript syntax for element attributes, which is not at all like HTML.

One of the things I really love about Wes Bos’ teaching style is that he guides the viewer towards the more familiar approach and implementation. I generally like to err on the side of simple, and less obfuscated code, though I understand that others like higher degrees of abstraction.

He suggests that we write our markup with JSX, which more closely resembles our friend, traditional HTML, so I found it to be much more clear. So, instead of this:

return React.createElement("p", {className: "foo"}, "Hello, world!");

We would write this:

return (<p className="foo">Hello, world!</p>);

Either works. But when we start to have more and more complexity in our markup, I found that the familiarity of HTML in the form of JSX served my understanding. Please keep in mind, though, that even with JSX, there are minor differences.

Myth #3: In order to try React, you have to understand all of the build tools.

It’s true that in order to use React, you need to use build tools, so this is usually what tutorials start with. I would recommend, though, that as a total beginner, you should muck around on CodePen, JSBin, or JSFiddle. It’s a good way to iterate quickly and learn before investing a lot of time in a brand new technology. For the purpose of getting us off the ground, I’ll be using CodePen for today’s examples.

Using React in Typical Applications

In typical applications using React 1.14+ we would start out by requiring React and ReactDOM:

var React = require('react');
var ReactDOM = require('react-dom');

And then call ReactDOM.render:

ReactDOM.render(routes, document.querySelector('#main'));

Using React in CodePen

In our case, we will simply be selecting React from the dropdown in the JS panel (click the cog icon at the top of the panel), and then using Babel as the compiler.

We don’t need to require React or React DOM, since we haven’t routed anything, we’re using the app component directly. Our code will be amended simply to:

React.render(<App/>, document.querySelector("#main"));
<div id="main"></div>

Also, in order to get started, you’re going to need the React Devtools extension for Chrome, or React Devtools Extension for Firefox which is great for debugging our virtual DOM.

If CodePen, you can use Debug View and it will find React properly:

Up and running

The basic building blocks that you need to know to make something is as follows:

// App
var App = React.createClass({
  render: function() {
    return (
      <div className="foo">Hello, world!</div>
    )
  }
});
 
React.render(<App/>, document.querySelector("#main"));

See the Pen Hello World React by Sarah Drasner (@sdras) on CodePen.

Let’s break this down. On the last line we find the main div id, and on that we render the component, which will load all of our React application. You can use your React tab in Devtools to now see the DOM element we’ve created.

We’ve attached as the first component. The first thing to note here is that this tag is capitalized β€” while this isn’t required it’s a best practice with React components. The second is that it is self-closing. Tags in react must be closed, either by an additional closing tag (e.g.

), or a self closing tag (e.g.


would become


). This is just how JSX works and an error will be thrown otherwise.

Note the structure for creating the app component at the top. You will get very used to this syntax moving forward, as everything we will build today will work off of this key building block.

The last thing to note is that rather than using class, we’re using className on the element. This is a gotcha if you’re used to writing HTML markup for a webpage. Luckily, it’s easily solved. For more information on JSX and its syntax, these docs are really good resources.

// App
var App = React.createClass({
  render: function() {
    return (
      <Header />
    )
  }
});
 
// Header
var Header = React.createClass({
 render: function() {
   return (
     <div className="foo">Hello, world!</div>
   )
 }
});
 
React.render(<App/>, document.querySelector("#main"));

See the Pen Hello World React by Sarah Drasner (@sdras) on CodePen.

In the next step, we’re extending app with a component. Header can be named whatever you would like it to be named, for clarity, we’ve named it the role it has in the document. You can see that if we also wanted to add a navigation element, that would be quite simple to add. Here’s the same page with just a standard bootstrap row of buttons for a

Categories: Designing, Others Tags:
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.