React Part III Flashcards

1
Q

What is lifecycle methods ?

A

Special methods built-in to React, used to operate on components throughout their duration in the DOM

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Lifecycle methods are called in what order ?

A
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
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is constructor ?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Renders()

A

This is where you determine what gets displayed to the page.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

componentDidMount()

A

It runs immediately after the component has mounted.

same as useEffect with an empty dependency array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

ComponentDidUpdate

A

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])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

ComponentWillUnmount

A
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
  }
}, [])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

useReducer

A

alternative to useState when you have complex state logic (take in state and action) as argument and return a newState

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

useCallback

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

useMemo

A

Returns a memoized value.

Pass a “create” function and an array of dependencies.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the difference between Element and Component?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Pure Components ?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

State

A

an object that holds some information that may change over the lifetime of a component

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Best practices for state

A

Keep state as simple as possible

Minimize the number of stateful components.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

how virtual dom work ?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly