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
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 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 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?
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
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
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:
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ông tồn tại (undefined) !!undefined = false
Tư duy đúng khi tổ chức React Component Design