1 of 40

React.js

2 of 40

3 of 40

Agenda for today

  • React Introduction
  • Setup for React
  • Components
  • Props
  • State

4 of 40

Disclaimer

5 of 40

  • Free and open-source JavaScript (JS) library used for building UI
  • Maintained by Meta and open-source community
  • Front-end web development
  • Widely used for websites: Netflix, Facebook, Uber, etc.

What's React?

6 of 40

  • ‘reacts’ to state changes
  • Updates parts of the page (individual components) without reloading the whole page → efficiency!
  • popular, good resources and community to help you
    • Static sites (Gatsby), Forms (Formik), etc.
  • In-demand skill for jobs

Why React?

7 of 40

  • JSX stands for JavaScript XML
  • JavaScript + HTML
  • Used within React as a JS syntax extension and allows you to write HTML-like markup within JavaScript

React: JSX

8 of 40

React: JSX Example

const num = 11;

const message = <h1>HOTH {num}</h1>

const nameFunction = () => {

return 'XI';

};

const message = <h1>HOTH {nameFunction()}</h1>;

  • {JavaScript expressions}
  • {JavaScript functions}

9 of 40

Playlist/list of songs

Bonus: (exposure to my amazing music taste) :D

Demo for Today

10 of 40

Agenda for today

  • React Introduction
  • Setup for React
  • Components
  • Props
  • State

11 of 40

  • Download
    • Node.js
      • allows for the development server that lets us see our changes to our code in real time
      • Recommended version
    • Text Editor
      • Visual Studio Code

Before we continue …

12 of 40

Set up your React project

In Terminal:

npm create vite@latest

13 of 40

Set up your React project

cd your-project-name

npm install

npm run dev

14 of 40

Demo

15 of 40

Agenda for today

  • React Introduction
  • Setup for React
  • Components
  • Props
  • State

16 of 40

  • Reusable code
  • Like the building blocks for your UI
  • JavaScript function
    • returns HTML (what you see as UI)
  • Function or class based components

Components

17 of 40

image

account info

actions

nav bar

comment box

reactions

18 of 40

breaking it up…

Profile Picture:

<img />

User Name:

<h5></h5>

comment:

<p></p>

time:

<p></p>

using HTML elements we can break this component even further!

19 of 40

Components in React

like a JavaScript function! (returning HTML)

20 of 40

Imports and Exports

  • Organization: components will be in different files
  • Must export at the end so that other files can access it

export default Song;

  • Must import at the top so that the file can access and use other files

import Song from './components/Song'

21 of 40

Note on Imports

  • the ‘.’ indicates to look within the directory you are in

  • the ‘..’ indicates to look outside of the directory

import Song from './components/Song'

import fullHeart from '../images/whitefullheart-removebg-preview.png'

vs.

22 of 40

oh look at that!

a great example of a component on a web page

Our Demo

23 of 40

Building our first component

24 of 40

Agenda for today

  • React Introduction
  • Setup for React
  • Components
  • Props
  • State

25 of 40

  • Motivation: think back to pinterest
  • For our playlist
    • Using the same format we want to display different information to diversify our songs
  • Props are passed (like parameters) to our function
    • allows us to pass in different data

Props

26 of 40

Using Props

props as an object

props in line

27 of 40

  • .map() function
    • 1: takes in each item of the array
    • 2: performs operation on the item
    • 3: puts new item after operation into a new array
  • Result: a new array with the each item after the operation was performed on it

array.map()

28 of 40

array.map()

29 of 40

Implementing props

30 of 40

Agenda for today

  • React Introduction
  • Setup for React
  • Components
  • Props
  • State

31 of 40

  • Contains data on property values of a component
    • Allows us to keep track of changes to a property
  • State can change (over time, from user interaction)
    • Component re-renders when state is updated
    • Ex: countdown,

collapsible menus

What is State?

32 of 40

  • useState()
    • a react hook!
      • functions that lets us “hook into” (use) React features like state

React Hooks

import React, {useState} from 'react'

33 of 40

variable that gets updated

State Variables

const [liked, setLiked] = useState(false)

function that will update the variable (update the state)

default/initial value we set the variable to

34 of 40

Updating State Variables

setLiked(!liked)

what you want to change the variable to

function to update the variable

35 of 40

updating state for our heart

State Demo

36 of 40

{liked ? fullHeart : emptyHeart}

Updating State Variables

condition

expression1

expression2

?

:

if condition is true, execute expression1, if not execute expression2

37 of 40

npm install @ionic/react ionicons

Using Icons

38 of 40

Result

39 of 40

Agenda for today

  • React Introduction
  • Setup for React
  • Components
  • Props
  • State

40 of 40

Thanks for watching!

good luck at HOTH!