readme - Default create-react-app README.md
- install
Node.js
- create react app via different approach
Approach | Steps |
---|---|
npx | $ npx create-react-app <project-name> |
npm | 1. $ npm install create-react-app -g 2. $ create-react-app <projct-name> |
Note: npx is a tool for executing Node packages
react-app
├── node_modules
├── package.json
├── package-lock.json
├── public
│ ├── favicon.ico
│ ├── index.html
│ └── manifest.json
└── src
├── index.js
├── ...
└── ...
- package.json: Contain the dependencies and the script required for the project
- package-lock.json: Ensure consistent installation of the dependencies
- node_modules: The folder all the dependencies are installed
- public folder
- manifest.json: Concerned with progressive web app
- favicon.ico: Icon we see in the browser tab
- index.html: The only html file we have in the application, it's mean we're building
single page applicatioin
- src
- index.js: the starting point of the application
- readme - What is jsx
- What should we know about the component
- handle
props
- create & update
state
-
Note: anytime we need to update state based on the previous state, always passing a function that will set the new state
e.g.
// class component this.setState(prevState => ({ count: prevState.count + 1 })) // functional component const [count, setCount] = useState(0) setCount(prevCount => prevCount + 1)
-
methods as props
conditional rendering
list rendering
handle input
Lifecycle
- handle
- Hooks: handle state and other React features in functional component
- Component template
- Component
- Pure Component - for class component extends
- React.PureComponent is similar to React.Component. The difference between them is that React.Component doesn’t implement
shouldComponentUpdate()
, but React.PureComponent implements it with a shallow prop and state comparison. - source code - Parent component
- source code - Pure component
- source code - Regular component: no performance improvement
- React.PureComponent is similar to React.Component. The difference between them is that React.Component doesn’t implement
- Memo Component - higher order component
React.memo -> react version ^16.6.0
- source code - Memo component
- Pure Component - for class component extends
- Hook
useCallback
in handle event methods - create a memoized version of the callback that only changes if one of the dependencies has changeduseMemo
in a variable - Returns a memoized value. It will only recompute the memoized value when one of the dependencies has changed.useCallback(fn, deps)
is equivalent touseMemo(() => fn, deps)
const memorizedCallback = useCallback(computeExpensiveValue(a, b), [a, b]) const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);
- source code - useCallback in handle event methods, useMemo in variables
- More
- About Refs
- class component: createRef()
- way 1: use createRef()
- way 2: use callback refs
- functional component: useRef()
- class component: createRef()
- Demo: basic use
- As the page loaded, by default, the input will be focused.
- Fetch the input value by click the button.
- source code - class component fous input while page loaded
- source code - functional component focus input
- Demo:
callback ref
- Instead
createRef
, use callback ref. - NOTE: callback ref is the element itself.
- source code - callback refs
- Instead
- Demo: Access the
refs with class component
- parent: ref -
refChildComponent
- children: method -
focusInput()
- source code - parent component
- source code - child component
- parent: ref -
- Demo:
React.forwardRef
, passing ref to children component- child component: return React.forwardRef(component)
- source code - parent component
- source code - child component
- Demo:
Hook - useRef()
, mutable ref, for variable in functional component
Render children into a DOM node that exists outside the DOM hierarchy of the parent component - ReactDOM.createPortal
- WHY use portal?
- when the child component is a
modal
, apop-up
, or atool-tip
- when the child component is a
- link - portal reference
- link - demo on CodePen
-
Higher Order Component
- Lift business logic to parent component and pass props to children for display
- Demo: Counter for
ClickCounter
andHoverCounter
- source code - wrapper component
- source code - ui component
-
Render Props
- The term
render prop
refers to a technique forsharing code
between React components usinga prop whose value is a function
- Core: React can use a prop whose value is a function to control what is actual render by a component
- source code - logic component
- source code - ui component
- The term
-
Custom Hook
- A custom Hook is basically a javascript function whose name start with
use
- A custom Hook can also call other Hooks if required
- useDocumentTitle
- useCounter
- useInput
- A custom Hook is basically a javascript function whose name start with
Context provides a way to pass data through the component tree without having to pass props down manually at every level.
steps
- Create the context
- Provide a context value
- Consume the context value
- http library: axios
- fake data from: JSONPlaceholder
- fetch data
- implement - fetch data while render component
- implement - fetch data while button click
- source code - class component
- source code - functional component