Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Just Plain React

Want to learn to build a reasonably functional app without having to learn all about JSX, Webpack, Babel, Redux, etc. Learn React to handle things like state management and routing. Build a simple React app without any extras today!

Jan 10, 2019 • 6 Minute Read

Summary

Many packages are commonly used with React to handle things like state management and routing. When you are new to React, it is overwhelming to try to learn all of this at once. Let's build a simple React app without any extras.

Hello World without JSX

Most people use JSX with React, but that requires Babel, which in turn means we need something like Webpack. If we don't use JSX, then we don't have to worry about any of that. Here's a JSX-free example to get us started.

      <!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Hello World</title>
    <script src="https://unpkg.com/react@16/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
  </head>
  <body>
    <div id="root"></div>
    <script type="text/javascript">
      var e = React.createElement;
      ReactDOM.render(
        e('h1', null, 'Hello, world!'),
        document.getElementById('root')
      );
    </script>
  </body>
</html>
    

The first argument to React.createElement is the element type (or your custom component, which we'll see later). The second is properties you can pass into that element (usually referred to as props). The third is child elements. In this case, we'll just display "Hello, world!" here, but we could put another element or component here or even a list of them.

Paste this into an html file and open it in your browser.

In case you're curious, JSX is syntax sugar so that you can write <h1>Hello, world!</h1> instead of React.createElement('h1', null, 'Hello, world!'). See React Without JSX for more info.

Components

React applications can be split up into Components. Here's the beginning of a TODO app that demonstrates components.

      var e = React.createElement;

function TodoItem() {
  return e("li", null, "Todo Item");
}

function TodoApp() {
  return e("div", null, [
    e("h1", { key: "title" }, "To Do List"),
    e("ul", { key: "todos" }, [
      e(TodoItem, { key: "item1" }),
      e(TodoItem, { key: "item2" })
    ])
  ]);
}

ReactDOM.render(e(TodoApp), document.getElementById("root"));
    

What's with the keys props?

A key is a unique string you must provide when creating lists of elements. You can read more about react keys, but I recommend you skip it for now.

Class-style Components without ES6

To get state and lifecycle hooks, you must use the class style for creating components. Most examples use ES6 classes for this, but that would mean we need Babel once again. To avoid this, pull in create-react-class with a little help from unpkg.com like this: <script src="https://unpkg.com/create-react-class@15.6.3/create-react-class.js"></script>. This will expose a function called createReactClass which we can use to create stateful components. Here's an example:

      var TodoItem = createReactApp({
  render: function() {
    return e("li", null, "Todo Item");
  }
});
    

This is a silly example, but we'll make better use of it in the next section.

Check out the React Without ES6 docs for more info on this subject.

Plain React State

Some kind of state library, such as Redux, is often pulled into React apps and it's a whole new layer to understand. Don't assume your app needs this. React's built in component state will suffice in many cases. It is recommended that you store your state in a top level component and pass that down to child components via props. Here's a simple TODO app that demonstrates this.

      var e = React.createElement;

function TodoItem(props) {
  return e("li", null, props.text);
}

var TodoApp = createReactClass({
  getInitialState: function() {
    return { items: [], text: "" };
  },

  handleEditInput: function(evt) {
    this.setState({ text: evt.target.value });
  },

  handleSubmit: function(evt) {
    evt.preventDefault();
    if (!this.state.text.length) return;
    this.setState(function(prevState) {
      return {
        items: prevState.items.concat({
          id: Math.random() + "",
          text: prevState.text
        }),
        text: ""
      };
    });
  },

  render: function() {
    return e("div", null, [
      e("h1", { key: "title" }, "To Do List"),
      e("input", {
        key: "input",
        type: "text",
        value: this.state.text,
        onChange: this.handleEditInput
      }),
      e(
        "button",
        { key: "add-todo-btn", onClick: this.handleSubmit },
        "Add ToDo"
      ),
      e(
        "ul",
        { key: "todos" },
        this.state.items.map(item =>
          e(TodoItem, { key: item.id, text: item.text })
        )
      )
    ]);
  }
});

ReactDOM.render(e(TodoApp), document.getElementById("root"));
    

What's Next?

You can now build a reasonably functional app without having to learn all about JSX, Webpack, Babel, Redux, etc. You may find that you want to add these things in, but you don't have to.

You are now equipped to take one of the following next steps:

  1. Build an app following the above patterns.
  2. Learn one more building block at a time, such as JSX, with a solid understanding of React to build upon.
  3. Try out something like create-react-app or NEXT.js if you want all the extras, but don't want to set them all up manually.