Objective: Install a simple, locally-hosted React app for every attendee and install a new node module
- Node.js
- npx
- IDE of choice
- Node.js: https://nodejs.org/en/
- npx:
npm i -g npx
-
cd into desired directory
-
Run create react app command
npx create-react-app my-app
-
Open my-app in your IDE
-
In IDE or native terminal, run command:
npm start
-
Go to localhost:3000
- Verify you see this screen
- [Image: image.png]
- Verify you see this screen
-
Open
src/App.js
-
Delete everything in
<div className="App"> </div>
-
Replace with
<h1> Hello World </h1>
- Verify changes are updating the React app
- [Image: image.png]
- Verify changes are updating the React app
-
Add a sample button with separate onClick function
<button onClick={handleClick}> click me! </button>
- UI:
- [Image: image.png]
- Console (F12)
- [Image: image.png]
- The console is also a really helpful, easy-to-use debugging tool
-
Sample NPM install
- Material UI
- Run command:
npm install @material-ui/core
-
Make sure React is installed
npm i react
-
Import React to project
ìmport React from 'react';
-
Create Material UI button
- Import Button
import { Button } from '@material-ui/core';
- New button
- Import Button
-
Add basic layouts for React directories
- Create a directory called
components
insrc
App.js
is the highest parent component. You use this to call other components. Typically, you would use React Router or similar to handle different paths. Much of that logic will remain inApp.js
- If you’re interested in this, I can go over a quick overview after this session
- Create a directory called
-
Create a new directory called
Home
- Create a new file in
Home
calledHome.js
- Create a new file in
Home
calledHome.css
- Create a new file in
-
In
Home.js
, paste this boilerplate code block-
import React from 'react'; import './Home.css';
class Home extends React.Component { constructor(props) { super(props); this.state = {} }
render() { return (
export default Home;
-
Explanation:
- Importing React again → always have to do this with every new
.js
file - Importing CSS file that we just created
- Creating a new React class component
this.state = {}
will contain all of your states in this component- All HTML will be housed in
return ()
- Importing React again → always have to do this with every new
-
-
Insert phrase in
div
tags inHome.js
-
Import
Home.js
intoApp.js
import Home from './components/Home/Home';
-
Render
Home.js
inApp.js
<Home/>
-
In summary, we’ve covered:
- Creating a new React app and all the boilerplate that comes with the project
- Running the React app on localhost
- Making updates to the HTML in our main
App.js
file - Creating a simple button with a function to control its logic
- Debugging using the console
- Importing and using new node modules into our project (Material UI)
- Creating a new React class component
- Importing this new class component into our
App.js
file
- Learn how to use functional components with hooks which can do most of what you’ll need without having to write class components
- Bootstrap themes to get slick UI components out of the box
- React Router to handle new URL routes
- Create a server directory to handle public and private API calls
- Tutorial here
- Find and install other node modules here
- Really should be using
.jsx
files, but I didn’t want to cover that here
-
Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
- sudo chown -R $USER /usr/local/lib/node_modules
-
Can’t find index.js file in material UI
- Make sure you’ve installed and imported React