Course site

IE 0 - Getting Started with React

Getting Started with React

The goal of this lab assignment is to walk you through installing and building your first React app.

The only hard prerequisite is to have previous knowledge of programming in any language. Basic knowledge of HTML and JavaScript is desired, but if you don’t have it, you will still be to do the assignment. All the code you need to finish this assignment is included in this document, so you can cut-and-paste, but try to follow what each bit of code is doing. Using Gen-AI/Agent, is not permitted for the individual assignments since they are designed for you to learn.

Several good HTML and JavaScript resources are available online and you can’t go wrong with these: HTML Guide and JavaScript Guide.

0 - Installing git (skip if you already have git installed)

Follow this link to install git according to your OS: git-scm.com/book/en/v2/Getting-Started-Installing-Git

Run git --version to make sure it is installed.

1 - Creating a git repository

Here we’ll initialize your app folder to be tracked by git. Start by creating a new folder for your work in this class. You could call it csc-3100-app. Then, in the terminal, cd into that folder and run git init:

cd csc-3100-app
git init

Now go to github.com and create a repository.

If it’s your first time on GitHub, you’ll need an account.

Note 1: DO NOT check initialize this repository with a README.
Note 2: You’ll turn in this repository at the end of the assignment, so leaving this as a public repo will make things easier.

If you make it private, you will need to invite the instructor to your repo so that I can check your files and grade the assignments (my githubid is laranicholsbrown).

2 - Installing Node.js and npm

We will be using npm to manage the separate packages (frontend, backend, etc.) that we will implement in our first app.

If you already have npm on your computer, skip this section.

Otherwise go to:

https://nodejs.org/en/download/

Install Node according to your operating system.

You can check that npm has been installed by running:

npm -version

3 - Initializing your monorepo

A monorepo is a git repository used to maintain multiple npm packages. The root of the monorepo is also an npm package.

To initialize your monorepo, go to your root directory (where you ran git init) and run npm init:

cd csc-3100-app
npm init

This command is interactive, but you can accept the defaults by pressing RETURN.

When the command is finished, there will be a file called package.json

You can always tell when a directory is an npm package because it will have a package.json.

Edit the package.json file and add the following lines just before the final closing brace. (You need to add the comma to the preceding line to keep it in valid JSON format.)

,
"workspaces": [
  "packages/*"
]

This tells npm that we are creating a monorepo and that our packages will be located in a directory called packages.

The last thing to do is create the packages directory. Once you do that, you are ready to create your first (sub)package in your monorepo.


4 - Scaffolding your Frontend

To build even a simple React app requires installing many dependencies and creating boilerplate files.

We will be using Vite (rhymes with “feet”) to build and develop our frontend, and Vite will provide the scaffolding we need.

The React frontend will be just one npm package inside our monorepo, and only this package will require Vite and React. (Our backend will be in a separate npm package and require NodeJS and Express.) All of the packages we create need to be in the packages directory we built in the previous step. So cd to there and then run an npm create command to initialize a new Vite package.

cd packages
npm create vite@latest react-frontend

This command will prompt you for more information about your new package. You should respond as follows:

? Select a framework:
❯ React

? Select a variant:
❯ JavaScript

Now, let’s verify this was installed properly. There should be a new directory under packages called react-frontend. Let’s cd into it and take a look at the package.json file. (If more does not work in your shell, you can open the file in your editor.)

cd react-frontend
more package.json

The package.json file is used by npm to manage all the packages you will use to build your project (react-frontend) in this case. There is a separate package.json file in each package, which allows npm to keep them separate, even though it uses a single node_modules to store them all. This is part of what we mean when we say we are using a “monorepo”; not only do we have all our code in one git repo, was are also having npm keep a single copy of all our dependencies in node_modules.

In your package.json file, you should see a "dependencies": section, as well as a "devDependencies": section, which will look something like this:

{
  ...
  "dependencies": {
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  },
  "devDependencies": {
    "@types/react": "^18.2.66",
    "@types/react-dom": "^18.2.22",
    "@vitejs/plugin-react": "^4.2.1",
    "eslint": "^8.57.0",
    "eslint-plugin-react": "^7.34.1",
    "eslint-plugin-react-hooks": "^4.6.0",
    "eslint-plugin-react-refresh": "^0.4.6",
    "vite": "^5.2.0"
  }
}

The "dependencies" section lists packages required to run your app.

The "devDependencies" section lists packages required to build your app. This will become important when you end up deploying your app, because only the runtime dependencies need to be deployed.

The command npm create vite has created the package.json file, but it may not have installed all the dependences. To do that, you need to run this command (in your react-frontend folder):

npm install

Once that is done, you’ll be able to start the scaffolded app. This has also been set up for you by npm create vite; there is an npm script in your package.json called "dev". To run it you can use the command:

npm run dev

You should see something similar to:

VITE v5.2.6 ready in 577 ms

➜ Local:   http://localhost:5173/
➜ Network: use --host to expose
➜ press h + enter to show help

Now you can go to your browser and enter the URL localhost:5173 in the Location bar. You should see a splash page with the Vite and React logos, and a button with a counter in it. Click on the button to see that it updates the count each time you click. You have just created and run a React app!

You will also see a line in the browser that says:

Edit src/App.jsx and save to test HMR

HMR stands for Hot Module Reload.

This means that whenever you edit and save any of the source files that Vite has processed, it automatically re-processes them, and in addition, tells a bit of code which it has installed in the browser to reload the modified module(s). This behavior is only available when you run npm run dev.

To see how Vite automatically compiles and updates your React code, find the text shown above in /src/App.js and replace it with your own text. and replace it with any other text. Once you save the file, you’ll notice that your browser page localhost:5173 automatically refreshes, and your new text appears. You will be doing this a lot as you’re developing your app; you can leave vite running while you’re editing your app, and it will automatically refresh.

When you’re done making changes, press Ctrl-C which will stop the app and return you to your terminal/command prompt. If you try and start the app without stopping it first (i.e, from another terminal), you’ll get a port conflict message from npm to remind you that vite is already running.


5 - Committing changes and pushing

Next, we’ll commit our changes. If you’re new to git, remember that, first, there are files where we never commit. For instance, the /node_modules folder of your app is part of your local npm installation. Every developer collaborating in your project would have their own /node_modules folder managed when they run their npm commands.

You will notice that there is a .gitignore file in your react-frontend directory. This file tells git which files it should ignore and not check in to the repository. The .gitignore file created by npm create vite will only be used when running git commands from inside the react-frontend folder.

Recall that with our monorepo, npm will put all of the dependencies from all of our packages in the same node_modules folder, in the root of the project. To make sure git never checks in that node_modules folder, you should create a .gitignore file in our root folder. Create the file using your code editor and enter the following line:

/node_modules

Now, to commit changes to the files we do want to track, we have to stage our files. Watch the supporting videos on Canvas and read the git documentation if you’re lost here.

First, stage everything in your app folder except the /node_modules (we already added it to our .gitignore file). The . character means current directory, so the following command will stage everything in the current directory (recursively).

# you should be in the root folder of your project
git add .

Check what is staged and what's unstaged. Anytime you’re lost, the git status command will be helpful.

git status

Once everything is staged, you’ll commit changes. Keep in mind that, with git, we commit locally, and only after that are we able to push one or more commits to a remote repository (e.g., GitHub). It’s a good practice to add a commit message to communicate what represents your code changes. Even if it’s obvious, place a clear message since, in general, in real-world projects, these messages can be used to understand the history of code changes and help maintenance activities.

git commit -m "First commit of my react app"

Connecting to GitHub

Now we’re almost ready to push changes to your remote GitHub repo. We need first to tell your local git repository that you have a remote host. Here’s the command to do it (you’ll also see this instructions on your GitHub repo page once you create the repo):

git remote add origin https://github.com/your-username/your-repo-name.git

This is the only time you have to run the git remote add command.

Now, locally in our local git repo, we’ll rename the master branch (which git init created by default) to main, which is what GitHub uses as the default branch. As a side note: technically, this is not a required step, but it’s one effort the software engineering community has taken to promote an inclusive and diverse culture. You can learn more about this important change here: https://www.independent.co.uk/tech/github-master-slave-slavery-whitelist-language-inclusive-a9568576.html

Rename the default branch:

git branch -M main

Finally, we’ll do the actual push to GitHub:

git push -u origin main

Once you push, you should be able to visit your GitHub repo online (github.com/your-username/you-repo-name) and you will see your commit there.


Creating a README on GitHub

To exercise one more useful command, on GitHub, create a readme file following their web interface. You’ll see a button saying “Create Readme”. Once you hit that button and create a readme file, GitHub will generate a commit triggered by its web interface. However, you don’t have this commit in your local repo.

So, go to the terminal and run git pull to bring the commit that generated the readme file to your repo (remember that git pull executes a git fetch and a git merge at once).

# This downloads the new commit and merges it locally
git pull

Now, the readme file is also local since you just synchronized your local repo with the remote repo.


Viewing commit history

To view your commit history:

git log

Together with git status, this command gives you a good understanding of the state of your repository.

Turn-In

Submit URL of your git repo to the Canvas Assignment.


Content adapted from Prof. Ken Kubiak.