Lecture 04 - Handling Interactions
Lecture 04 - Handling Interactions
Objectives
◆ Handle user-initiated events
◆ Make components “remember” information with state
◆ Update the UI in two phases
◆ Update multiple state
◆ Update an object in state
◆ Update an array in state
2
Adding event handlers
◆ Handling events is similar to the way you handle events on DOM
elements:
◆ onClick instead of onclick. (camelCase)
◆ onClick={save} instead of onclick="save()"
3
Event Handler Example (1)
4
Event Handler Example (2)
5
State
◆ State is a component’s memory
◆ Using a useState Hook to add state to a component
◆ Hooks are special functions that let your components use React
features (state is one of those features)
6
useState Hook
◆ Iakes the initial state variable
◆ returns a pair of values: the current state, and a state setter function
that lets you update it.
7
Exercise 01
1. Login Form
username = pass: alert thành công
2. Colors
3. Counter
4. Student Info
5. Clock
8
Exercise 02 - Menu
9
Exercise 03
10
React batches state updates
◆ React waits until all code in the event handlers has run before
processing your state updates.
11
Updating Objects in State
12
Object spread syntax
◆ update only one field in a form,
◆ keep the previous values for all other fields.
13
Exercise 03
◆ Updating a nested object
14
Updating Arrays in State
◆ How to add, remove, or change items in an array in React state
◆ How to update an object inside of an array
15
Topics
1. React Router
2. React-Bootstrap
3. Tailwind CSS
4. JQuery
5. Redux
6. Angular
16