React Part III Flashcards
What is lifecycle methods ?
Special methods built-in to React, used to operate on components throughout their duration in the DOM
Lifecycle methods are called in what order ?
class App extends Component { constructor() { // here is our constructor lifecycle method }
componentDidMount() { // here is anything we want to happen // immediately after the component renders // (e.g. grab our AJAX data) }
componentDidUpdate() { // here is anything we want to happen // when a component is updated // (e.g. when props or state change) }
render() { // here are any DOM elements // we want to display on the page } }
What is constructor ?
A constructor is a method that is called automatically when we created an object from that class. It can manage initial initialization tasks such as defaulting certain object properties or sanity testing the arguments passed in.
Renders()
This is where you determine what gets displayed to the page.
componentDidMount()
It runs immediately after the component has mounted.
same as useEffect with an empty dependency array
ComponentDidUpdate
When a component updates when a prop or states change
similar to useEffect with [depencies array] (useEffect can seperate)
componentDidUpdate() { // Some updating logic // Some other updating logic }
// The above is roughly(!) the same as: useEffect( () => { // Some updating logic }, [someDependency])
useEffect( () => { // Some other updating logic }, [anotherDependency])
ComponentWillUnmount
Just before a class component is unmounted from the DOM same as useEffect cleanup
componentWillUnmount() { // Some code to run before the component unmounts }
// The above is the same as: useEffect( () => {
return () => { // Some code to run before the component unmounts } }, [])
useReducer
alternative to useState when you have complex state logic (take in state and action) as argument and return a newState
useCallback
return a memoized callback function (look like useEffect with [dependencies], that only changes if one of the dependencies has changed
- use to prevent unnecessary re-render(shouldComponentUpdate)
useMemo
Returns a memoized value.
Pass a “create” function and an array of dependencies.
What is the difference between Element and Component?
An Element is a plain object describing what you want to appear on the screen - createElements return an object (cant be mutated) - render to screen using render()
Components can be declared in different ways: class, function, take props as input and return JSX tree as output
Pure Components ?
exactly same as react components except that it handles shouldComponentUpdate.
Pure component do comparison on both props and state
Component wont compare, default re-render whenever shouldComponentUpdate is called
State
an object that holds some information that may change over the lifetime of a component
Best practices for state
Keep state as simple as possible
Minimize the number of stateful components.
how virtual dom work ?
3 steps:
When data change, ui is rerended using virtual dom
Differences between virtual dom and previous dom is calculated
Once calculations are done, real dom will be updated with change