Setup React JS (using NPM and Vite)

Purpose: In Module 5, we change our strategy from server-side rendering to single-page client-side rendering. We will use React JS, a popular front-end library. React JS code needs to be translated to ordinary Javascript before being deployed in a website, and the translating engine is written in Node JS, a back-end Javascript library. You will develop React only on localhost, and you will deploy only the translated code. So although we are not teaching Node JS, strictly speaking, we will install Node JS and use it to translate React JS code into ordinary Javascript. We will also use Node to run a dev server to test and debug.

Install Node and PNPM

Install Node by going to nodejs.org/en/download. Scroll past the docker window and get a prebuilt Node.js (there is a dropdown to select operating system and a button to download).

Install a Node package manager from pnpm.io/installation. There are three (NPM, PNPM, Yarn), but I use PNPM for space efficiency (React builds use 100MB minimum in the node_modules directory per project, but PNPM converts that directory into sym links so that the 100MB only takes up space once no matter how many React projects you set up).

Create a React JS Project Using Vite

Although original react projects were created using a Node package called create-react-app, that has been deprecated. The current supported way to do it is to use a Node package called Vite, which runs React and other types of projects as well. To create a React project in Vite, run the following from the command line after installing Node and PNPM:

pnpm create vite@latest <your_project_directory_name> --template react

Take some time to look at the files. Some notable ones are:

package.json (works a lot like requirements.txt in a Python project, managing Node JS dependencies)
vite.config.js (sets configuration variables for the translator that turns React JS into ordinary JS)
.gitignore (the most important line in the default .gitignore for a React JS project or any Node JS project is the one that excludes the 100MB node_modules file from getting into your git repo. Never let node_modules into a git repo!)
index.html (Starter HTML for when your react project is finally translated. Rarely changed but can be if needed.)
public/* (similar idea to the "static" directory in a Flask project; static content can go here)
src/main.jsx (this is where your React code starts; we rarely modify it)
src/App.jsx (main.jsx imports this code; this is where your code typically starts)
src/index.css (global CSS file that applies to all components in the project)
src/App.css (local CSS in App.jsx and child components)

Practice Running the React Dev Server on the Default React Project

Most debugging in React JS is best done on the React Dev Server. Practice running this server by using the following command from the project directory:

pnpm run dev

While running, open Developer Tools and follow the link to download the React Developer Tools. You need good error messages with React, and Chrome's standard developer tools, while they are not terrible, are much less clear for React projects. Downloading React Developer Tools gives you much clearer error messages, and they integrate seamlessly with Chrome's Developer Tools (and several other browsers).

Practice Building the Default React Project in Document Root

Before a React project is translated into ordinary Javascript, you must install the dependencies using the following command from the command line:

pnpm install

Translate the React project into ordinary javascript by running the command:

pnpm run build

The translated content (all stuffed into one file, index.js) should be in the dist/ folder, along with index.html and the contents of your public/ folder. Copy or move the contents of the dist/ folder into your document root directory and then run it from your webhost (it must be in the document root until you make the modification below).

Modify to Run in a Subdirectory, then Rebuild

React projects, by default, assume they will be deployed to the document root directory. During the translation process, paths to certain assets are converted into absolute paths in dist/index.js. If you intend to run a react project from a subdirectory (which I usually do), you must make a modification to vite.config.js to change this default behavior, setting the "base" variable to "./", as follows:

import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' // https://vite.dev/config/ export default defineConfig({ base: './', plugins: [react()], })

Then rebuild using pnpm run build.

Policy: Publishing Your React Code

In this class, you will upload your React source code to your private Git repository, but you will ONLY upload the contents of your dist/ folder to the public part of the webhost.

NEVER upload your source code to anywhere in your public_html directory. This is important in school to prevent cheating, and it is important in business to avoid revealing your source code.

The reason it is not cheating to upload the translated javascript is that the resulting dist/index.js is very hard to read and somewhat hard to manipulate even using developer tools like Chrome's, so much that people have analogized translating React code to compiling to machine code. You will sometimes hear the contents of the translated index.js called "transpiled" code, a mix of "translated" and "compiled."

Download and Build a Sample React App

Now that you have practiced running Vite's default React app, please download the following sample project, which has both a flaskapp/ and a reactapp/ directory.

React Batpage

The Flask App (which is basically the same as the one you built in Exercise 4.4 except but with only the alert_ajax route). To connect the Flask App to the React App, open reactapp/src/axiosInstance.jsx and look at line 4 which sets the attribute baseURL. Currently, it points to localhost/flaskapp. Either copy the flask app to run in that directory or change baseURL so it points wherever you are running the contents of the flaskapp directory.

Practice running the react portion of this app using pnpm install, pnpm run build, and pnpm run dev.