React Node Tutorial

Posted on  by admin

This tutorial doesn’t assume any existing React knowledge. We will build a small game during this tutorial. You might be tempted to skip it because you’re not building games — but give it a chance.

Overview

The techniques you’ll learn in the tutorial are fundamental to building any React app, and mastering it will give you a deep understanding of React. This tutorial is designed for people who prefer to learn by doing. If you prefer learning concepts from the ground up, check out our step-by-step guide.

You might find this tutorial and the guide complementary to each other. The tutorial is divided into several sections:. Setup for the Tutorial will give you a starting point to follow the tutorial.

Overview will teach you the fundamentals of React: components, props, and state.

Completing the Game will teach you the most common techniques in React development. Adding Time Travel will give you a deeper insight into the unique strengths of React.

You don’t have to complete all of the sections at once to get the value out of this tutorial. Try to get as far as you can — even if it’s one or two sections. In this tutorial, we’ll show how to build an interactive tic-tac-toe game with React.

You can see what we’ll be building here: Final Result. If the code doesn’t make sense to you, or if you are unfamiliar with the code’s syntax, don’t worry!

The goal of this tutorial is to help you understand React and its syntax. We recommend that you check out the tic-tac-toe game before continuing with the tutorial.

One of the features that you’ll notice is that there is a numbered list to the right of the game’s board. This list gives you a history of all of the moves that have occurred in the game, and it is updated as the game progresses.

Tools You Will Need

You can close the tic-tac-toe game once you’re familiar with it. We’ll be starting from a simpler template in this tutorial. Our next step is to set you up so that you can start building the game. We’ll assume that you have some familiarity with HTML and JavaScript, but you should be able to follow along even if you’re coming from a different programming language. We’ll also assume that you’re familiar with programming concepts like functions, objects, arrays, and to a lesser extent, classes.

React Quiz

If you need to review JavaScript, we recommend reading this guide. Note that we’re also using some features from ES6 — a recent version of JavaScript. In this tutorial, we’re using arrow functions, classes, let, and const statements.

Before We Start the Tutorial

You can use the Babel REPL to check what ES6 code compiles to. There are two ways to complete this tutorial: you can either write the code in your browser, or you can set up a local development environment on your computer.

This is the quickest way to get started! First, open this Starter Code in a new tab. The new tab should display an empty tic-tac-toe game board and React code.

We will be editing the React code in this tutorial. You can now skip the second setup option, and go to the Overview section to get an overview of React. This is completely optional and not required for this tutorial! This setup requires more work but allows you to complete the tutorial using an editor of your choice.

Here are the steps to follow:. Make sure you have a recent version of Node.js installed.

Step 5: Deploy your app to the web with Heroku

Follow the installation instructions for Create React App to make a new project. Delete all files in the src/ folder of the new project . Don’t delete the entire src folder, just the original source files inside it. We’ll replace the default source files with examples for this project in the next step. Add a file named index.css in the src/ folder with this CSS code.

Add a file named index.js in the src/ folder with this JS code.

Setup for the Tutorial

Add these three lines to the top of index.js in the src/ folder:. Now if you run npm start in the project folder and open http://localhost:3000 in the browser, you should see an empty tic-tac-toe field.

We recommend following these instructions to configure syntax highlighting for your editor. If you get stuck, check out the community support resources.

In particular, Reactiflux Chat is a great way to get help quickly. If you don’t receive an answer, or if you remain stuck, please file an issue, and we’ll help you out. Now that you’re set up, let’s get an overview of React!

Want to build real-world apps like YouTube, Instagram, and Twitter with React? Here's how.

React is a declarative, efficient, and flexible JavaScript library for building user interfaces.

It lets you compose complex UIs from small and isolated pieces of code called “components”. React has a few different kinds of components, but we’ll start with React.Component subclasses:.

Step 4: Make HTTP Requests from React to Node

We’ll get to the funny XML-like tags soon. We use components to tell React what we want to see on the screen. When our data changes, React will efficiently update and re-render our components.

Completing the Game

Here, ShoppingList is a React component class, or React component type. A component takes in parameters, called props (short for “properties”), and returns a hierarchy of views to display via the render method. The render method returns a description of what you want to see on the screen. React takes the description and displays the result. In particular, render returns a React element, which is a lightweight description of what to render.

Most React developers use a special syntax called “JSX” which makes these structures easier to write. The

syntax is transformed at build time to React.createElement('div').

The example above is equivalent to:. If you’re curious, createElement() is described in more detail in the API reference, but we won’t be using it in this tutorial. Instead, we will keep using JSX. JSX comes with the full power of JavaScript. You can put any JavaScript expressions within braces inside JSX.

Each React element is a JavaScript object that you can store in a variable or pass around in your program. The ShoppingList component above only renders built-in DOM components like

and
  • .

    But you can compose and render custom React components too. For example, we can now refer to the whole shopping list by writing .

    Create React App

    Each React component is encapsulated and can operate independently; this allows you to build complex UIs from simple components. If you’re going to work on the tutorial in your browser, open this code in a new tab: Starter Code. If you’re going to work on the tutorial locally, instead open src/index.js in your project folder (you have already touched this file during the setup).

    Step 2: Create an API Endpoint

    This Starter Code is the base of what we’re building. We’ve provided the CSS styling so that you only need to focus on learning React and programming the tic-tac-toe game. By inspecting the code, you’ll notice that we have three React components:. The Square component renders a single