Skip to content
youhoc
  • Pages
    • Home
    • Modern App Guidelines
    • Linux
      • Day 1: Linux Distributions & Navigation
      • Day 2: User Management
      • Day 3: File Permission & Ownership
      • Day 4: Package Management
      • Day 5: Services Management
    • Javascript
      • JS The Weird Part
        • Execution Context
        • Types & Operators
        • Objects & Functions
        • Error Handling & Strict Mode
        • Typescript, ES6, Tra
      • Modern JS
        • JS in the Browser
        • Data Storage JSON
        • Modern JS
        • Advanced Objects & Methods
        • Webpack & Babel
        • Async
      • jQuery
        • In-depth Analysis of jQuery
      • React-ready JS
        • Arrow Function
        • Template Literals
        • Logical AND, OR, Ternary, Nullish Operators
        • Destructuring & Rest Operator
        • Array Method
        • Immutability and Spread Operator
        • Promises, Async/Await, Callback
    • PHP
      • gruntJS
      • composer
      • MySQL
    • Docker
      • Container Basics
      • Container Networking
      • Container Image
      • Container Volume & Persistent Data
      • Dockerfile
      • Docker Compose
      • Docker Registry
    • Node.js
      • 1. Installing & Exploring
      • 2. Core Modules
      • 3. Get User Input
      • File System & Input Arguments
      • 5. Express Web Server
      • 6. Deploy to Heroku & Github
      • Authentication
      • 7. Databases
      • 8. Rest API
    • ReactJS
      • React from Andrew
        • Summary from Next
        • 1. Basics
        • icon picker
          2. React Components
        • 3. Webpack
        • 4. Styling with SCSS
        • 5. React Router
        • 6. React Hook
      • Modern React From The Beginning
        • Intro to JSX
        • Vite Build Tools
        • Basic Component Creation
        • Component State
        • Props & Component Composition
        • useState with Inputs & Form Submission
        • useEffect, useRef & Local Storage
        • Async / Await and Http Request in React
        • React Router: Declarative Mode
        • ContextAPI
        • React Router: Framework Mode
          • File-routing & HTML Layouts
          • Server-side Data Query
          • Links & Navigation
          • Loaders
    • Typescript
      • Type User vs UserProp
    • Payload CMS

2. React Components

Class Components

Thinking Components

ES6 Classes

Normal Class

Extended Class

Default Class Syntax

Component Name must be Uppercase
lowerCase components will not be displayed
because lowerCase tags are HTML tags
You can render as many components in JSX as you like
Components just like include(php)

Nested Components

Component Props

Data & Variables = Props
How do we pass data across Components?
pass from above components
receive with this.props
is an object

Component Events & Methods

Define a function inside a Component

Assign a function without a ()
but how to insert params?

Notes about this binding

Class Component custom methods will lose binding with its props
Because this.addNew (in red) loses its context with the addNew (in yellow) function.
addNew actually still can access this.props
but addnew loses its content when we assign this.addNew = addNew()
So the solutions are

Component State

When data in a Component has changed, it will re-render automatically, not manually as we did.
Re-render with new State value
Screen Shot 2022-03-13 at 12.41.45 PM.png
Setup default State Object
Render with default state
Change state object with setState
DO NOT JUST CHANGE state values this.state.count++
access previous state values with prevState
return a new state object
Component will be automatically re-rendered
Start again with a function inside class

setStage example

ALWAYS pass in a whole function
the old way was to pass in an object only, but it is NOT RECOMMENDED

Manipulate Children Components from Higher Components

Not only you can pass Props
You should also pass Functions for Children Components to use
write function at highest component
bind ‘this’ value
pass to Component
receive at this.props.functionName at component levelt
DO NOT WRITE functions at lower Children Components
Changes from higher Components will also cause re-render auto

Pass data up-stream?

How to pass data from form submit and process it?
It’s super complicated
USE 2 FUNCTIONS, 1 at Higher Component and 1 at Local Component

1. Create a function at Higher Componnent
remember to bind it and pass it to Component
2. Create another function at child component
to process local data and then pass it to higher function
remember to bind it as well
be careful to us this.functionName (local) and this.props.functionName (higher)

3. Use concat to create new state array value
without modifying prevState value (NEVER)
4. Bonus: Data Validation
Data validation at child or higher component?
Teacher does validate at higher component and pass error to child one
How about validating at child component?
Validation at Parent Component

Summary

Screen Shot 2022-03-13 at 4.21.19 PM.png

Default Props for Class Components

To pass props from even higher components

Lifecycle Methods

Difference between Class & Functional
Lifecycle Methods only available in Class Component
To process data between loading and saving

Saving & Loading Data

Learn about Local Storage in
Broken link
=>
Broken link
Only Save Data when things changed
should code save data first and check if data is stored correctly

Get Data the right way

===

Functional Components

Definition of Stateless Function Components

Components: you can mix both Class-based or Function-based
Function: defined by Functions
you have to pass in props and use them
no need for ‘this’ object
Stateless: Functional Components do not allow state
Functional Components are for simple, presentational components

Default Value for Props (Functional Component)

What happen when we do not pass value to Component?
it will use default values
For example, initial values for empy database, empty state

Passing Data to Local Function

Using props to pass top-down methods to lower functional components

!!!ES6 Class Properties



!!!useState (React Hook)

Manage state for Functional Component

States in FC can be an object, OR number, string, anything
in CC, state must be an object
useStage always exports 2 things: states, stateAction
use Array destructuring to get those 2 exported things

Passing variables to Components as normally.

useState vs setState


With React Hook, React NOW WANT you to use useStage multiple times to track multiple state values
What if we use an object to manage multiple state values?
When we change state values, we have to declare the WHOLE OBJECT {count: ...,name:..., otherState:...}
When we call any value, we have to provide object name state.things (longer code)
Even though you can overide with ...state
A full explaination here:

!!!useEffect

Manage Lifecycle for FC

Similar to:
componentDidMount
componentDidUpdate
componentWillUnmount
useEffect run everytimes a Component has changed (useState)
run 1st times = componentDidMount
run every change state = componentDidUpdate
useEffect to sync state, props across compoennts

useEffect with Condition


useEffect Dependencies

Add 2nd call to useEffect
unlike CC, all methods must go to under 1 componentDidMount or DidUpdate

Clean-up useEffect

componentWillUnmount

===

3rd-party Components

Adding custom child component
React Modal

useReducer

Similar to connect() function by Redux
manage state and action methods and a single place
then dispatch action in FC

useState actually use useReducer behind the scene.
useState is a simple form of useReducer
useState depends on useReducer to work
When we dispatch actions and the state changes our component re rendered with the new data.
useState for local change
useReducer for cross-component change

useContext

Passing props values through components just like Redux:
data to be render, function to be called
Redux Store / Provider / Connect function

Custom Hook

Khó quá, ko học nổi. Chưa biết làm gì với nó.

Side-note

Convert an empty string to booleen
khác 0
khác rỗng
không tồn tại (undefined)
!!undefined = false

Tư duy đúng khi tổ chức React Component Design

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.