1. Trang chủ
  2. » Tất cả

React 03 props, refs state

28 2 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề React Props, Refs & State
Thể loại lecture notes
Định dạng
Số trang 28
Dung lượng 2,38 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

React Component Props, Refs & State... Creating Login component  3.2.. Creating resuable component... Creating Login component Create a new file named “Login.js” in “src” folder... Cre

Trang 1

React

Component

Props, Refs & State

Trang 2

1 Component?

2 Why Component?

3 Creating Components

4 Props & State?

5 Working with Props

6 Refs & the DOM

7 Component Lifecycle

8 Working with State

Trang 3

1 Component?

 HTML, CSS & JavaScript are about building user interfaces

as well React makes building complex, interactive and

reactive user interfaces simpler

 React is about “Components”

 Components let you split the UI into independent, reusable pieces, and think about each piece in isolation

 Components are like JavaScript functions They accept

arbitrary inputs (called “props”) and return React elements describing what should appear on the screen

Trang 5

2 Why Component?

Trang 6

3 Creating Components

 3.1 Creating Login component

 3.2 Creating resuable component

Trang 7

3.1 Creating Login component

 Create a new file named “Login.js” in “src” folder

Trang 8

 Copy all JSX codes from App.js to Login.js

 Modify App.js file

Trang 9

3.2 Creating Reusable Component

Trang 10

Creating Input Component

 Create a new folder named “components”

 In “components” folder, create a new file named “Input.js”

 Create a new class component named “Input” as below

Trang 11

4 Props & State?

 “props” is short for “properties”

 props are passed into the component (like parameters)

 state is managed within the component (similar to variables)

 Both props and state are plain JS objects

 Both props and state changes trigger a render update

 Changing props and state

props state Can get initial value from parent Component? Yes Yes Can be changed by parent Component? Yes No

Can set initial value for child Components? Yes Yes

Trang 12

5 Working with props

Trang 13

Passing Props in Login Component

Trang 14

Controlling Label Width

Trang 15

Supporting Multiple Rows

Trang 16

6 Refs & the DOM

 Refs provide a way to access DOM nodes or React elements created in the render method

 There are a few good use cases for refs:

• Managing focus, text selection, or media playback.

• Triggering imperative animations.

• Integrating with third-party DOM libraries.

 Don’t Overuse Refs

Your first inclination may be to use refs to “make things happen” in your app If this is the case, take a moment and think more critically about where state should be owned in the component hierarchy.

Trang 17

Working with DOM

• In “Login.js” file, modify following codes

Trang 18

 In “Input.js”, add “inputRef” property

Trang 19

Handling Form Submission

Trang 20

Testing

Trang 21

7 Component Lifecycle

Trang 22

What to do/not to do with lifecycle

constructor

– Do

• set initial state

• if not using class properties syntax — prepare all class fields and bind functions that will be passed as callbacks

Trang 23

componentDidUpdate(prevProps, prevState, snapshot)

Trang 24

Using componentDidMount Event

Trang 25

8 Working with State

Trang 26

Getting State Value

Trang 27

Testing

Trang 28

THE END

Ngày đăng: 09/02/2023, 15:34

w