1 of 49

Events & State

Presenter: Kaylin

2 of 49

Please fill out this sign-in form!

3 of 49

Membership Code

hackschool5

members.uclaacm.com

4 of 49

Curriculum

  • Week 2: Intro to HTML/CSS
  • Week 3: Intro to TypeScript + Vite
  • Week 4: Intro to React
  • Week 5: React Events & State Management
  • Week 6: Navigation
  • Week 7: Tailwind CSS, Linters, VSCode Settings
  • Week 8: Deployment & Showcase

5 of 49

A Note on Notes:

We recommend not coding along during this workshop

(besides during our 5-min checkpoints)

After the workshop we will be having a work session

where you can practice with the help of our mentors!

Slides can be used for reference.

Demo will be available on Github.

6 of 49

7 of 49

Outline

  • Recap
  • Arrow functions
  • Callbacks
  • Array destructuring
  • Events
  • State

8 of 49

Recap:

our app.tsx:

this is a component!

9 of 49

Recap:

our app.tsx:

these are props!

10 of 49

Our goal today 🙌🏼 🙌🏼 🙌🏼

11 of 49

Arrow Functions

Arrow functions are specially structured functions that

  • don’t need curly braces ‘{}’ or the ‘return’ keyword
  • simple and improved readability!
  • convenient to pass into other functions!

12 of 49

Arrow Functions

// Traditional function expression

function square(num: number): number {

return num * num;

};

// Arrow function

const square = (num: number): number => num * num;

VS

13 of 49

Callbacks

  • A callback is a function passed as an argument to another function
  • They are called at the end of the original function
  • Arrow functions are commonly used as callbacks

14 of 49

Callbacks

callback

15 of 49

Array Destructuring

Remember arrays from the last workshop? We can actually do something pretty interesting with them! Take a look at the code below.

let phrase: string[] = ["Hack", "Is", "Awesome"];

16 of 49

Array Destructuring

We can destructure it from an array into individual variables.

function officers(): string[] {

return ["Aazel", "Sneha", "Kaylin"];

}

let [a, b, c]: [string, string, string] = officers();

17 of 49

Array Destructuring

Now, the variables have values respective to the order of the array and the listed variables!

console.log(a); // Output: Aazel

console.log(b); // Output: Sneha

console.log(c); // Output: Kaylin

18 of 49

Now what was the point of all that…🤔

Arrow functions

  • simplifies defining event handlers

Callback

  • helpful for updating state

Array Destructuring

  • simplifies useState syntax (more to come on this)

19 of 49

Now, let's get into our main topic!

20 of 49

How do Buttons work?

As we saw from our goal, we click on the button, then something occurs.

The button click from the user triggers some sort of action to occur!

21 of 49

TypeScript Events

This is called an event!

TypeScript reacts to events (such as a user clicking a button) and performs a desired action.

22 of 49

Examples of Events in React

onClick

onKeyDown

23 of 49

A helpful analogy!

24 of 49

A helpful analogy!

event

25 of 49

A helpful analogy!

event handler

event

26 of 49

Event Handlers

We need to specify what event actually occurs when the button is clicked!

function handleClick() {

alert("u clicked");

}

27 of 49

How to use Event Handler Functions?

We make sure this function is called through the event property!

<button onClick={handleClick} />

event

event handler

28 of 49

Events Demo

29 of 49

🚨5 Minute Checkpoint🚨

Implement your event!

30 of 49

What is a static website?

Typically when something is already on the screen, it's like pen on paper. You can't remove/change it. We say it is static.

31 of 49

But

… if we could re-render the site (have the app refresh itself), then we can see the changes! Maybe there's some salvation here!

32 of 49

Another problem…

The problem is, using just a TypeScript variable by itself isn't enough to do this. We need some bothersome TypeScript code to do that…

What if I told you that React can help us fix this with relatively minimal work?

33 of 49

React State

Think of state as a piece of data that belongs to the component that can be updated when necessary

React re-renders that component for us when the state updated!

34 of 49

Small Aside: Hooks!

useState is an example of a React hook.

Hooks let us "hook into" parts of React, such as the state of the component.

35 of 49

Small Aside: Hooks!

React simplifies the complicated details of JavaScript or TypeScript by handling a lot of it for you, and giving you a "blueprint" for building components!

36 of 49

New Import Statement 👀

To start using State, we have to import it from React!

import React, { useState } from "react";

37 of 49

Aside: useEffect

import React, { useState, useEffect } from "react";

useEffect(() => {

console.log(The like count changed to: ${likes}”);

}, [likes]);

38 of 49

~Aside~ Aside: Custom Hooks!

39 of 49

Creating State Variables

We define the state variables before the return field (where our content is)! This can be used with any TypeScript type, not just numbers!

function App() {

const [count, setCount] = useState<number>(0);

return (...);

}

40 of 49

Creating State Variables

useState is a function!

function App() {

const [count, setCount] = useState<number>(0);

return (...);

}

remember this? array destructuring!

let's see what useState gives us

41 of 49

Creating State Variables

function App() {

const [count, setCount] = useState<number>(0);

return (...);

}

the actual variable that can get updated

42 of 49

Creating State Variables

function App() {

const [count, setCount] = useState<number>(0);

return (...);

}

a function that updates the variable for us to whatever value we give it

43 of 49

Creating State Variables

function App() {

const [count, setCount] = useState<number>(0);

return (...);

}

some initial value.�can be any TS type!

44 of 49

Updating State Variables

All we have to do is call the updating function, like setCount, with some value to assign!

setCount(count + 1);

45 of 49

Updating State Variables

console.log(count); // Output: 0

setCount(count + 1);

console.log(count); // Output: 1

46 of 49

State Demo

47 of 49

🚨5 Minute Checkpoint🚨

Implement state!

48 of 49

Time to Work On Your Own Website! Thank you for coming!

49 of 49

Staying Updated

Instagram: @uclahack

Hack Website: hack.uclaacm.com

^ click the shiny glowing banner :0

Discord: discord.gg/gwaaFS2fah