16
loading...
This website collects cookies to deliver better user experience
Each component in React has a lifecycle which you can monitor and manipulate during its three main phases.
The three phases are: Mounting, Updating, and Unmounting.
Mounting means putting elements into the DOM.
The next phase in the lifecycle is when a component is updated. A component is updated whenever there is a change in the component's state or props.
The next phase in the lifecycle is when a component is removed from the DOM, or unmounting as React likes to call it.
React.Component
, function components do not need to do so
By using array destructuring, we can give any name to the variables in the array.
If we are using object destructuring, we have to use the identical name to the retrieved object's property name.
Only Call Hooks at the Top Level, Do not call Hooks in loops, conditions, or nested functions
Only Call Hooks from React Functions, Do not Call Hook in Javascript's functional component.
useEffect
will be called asynchronously during rendering that runs after react has rendered all the components to
ensures that effect callback does not block browser painting. It change the DOM after rendering which results to screen to blinking.
useLayoutEffect runs synchronously immediately after React has performed all DOM mutations and then proceed to rendering, hence avoid using it with heavy calculation callbacks which may block the UI display. It can be useful if you need to make DOM measurements such as the scroll position or DOM mutations.
Class Components | Hooks |
---|---|
getDerivedStateFromProps |
useState 's update function |
shouldComponentUpdate |
useMemo |
componentDidMount |
useEffect with empty dependency |
componentDidUpdate |
useEffect |
componentWillUnmount |
useEffect 's return function |
return React.createElement(
'div',
null,
`Hello ${this.props.toWhat}`
);
React.createElement()
method. Using XML will have a better readability.From parent to child components: Use props to pass data.
From child to parent components: Use props to pass the callback function and let the child component to call the function.
Use context or Redux to handle global states cross levels.
Use Event Publisher/Subscriber: The publisher and subscriber are unaware of each other. All the communication between them is taken through events which are emitted from the publisher and notifies subscriber.
Based on HTML5 history routing: To change the url, we can use history.pushState and replaceState to push the URL onto the history stack, and at the same time, we can apply APIs such as history.go(). Monitoring url changes can be triggered by custom events
Hash-based routing: Listening to hashChange
event. We can directly change hash by assign variable to location.hash
Use the <Route>
Component
Use <Switch>
with <Route>
Use <Link>
、 <NavLink>
、<Redirect>
components
<Redirect>
component