React Flashcards

1
Q

Error boundaries

A
React components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of the component tree that crashed. 
Error boundaries catch errors during rendering, in lifecycle methods, and in constructors of the whole tree below them.
eg: used in class components as of react v17, with help of componentDidCatch()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Hook rules

A

Hooks can only be called inside React function components.
Hooks can only be called at the top level of a component.
Hooks cannot be conditional

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

React Classes

A

Classes can have it’s own methods and Inheritance.

where super() method refers to the parent class.

By calling the super() method in the constructor method, we call the parent’s constructor method and gets access to the parent’s properties and methods.

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

Arrow functions

A

regular function, this represents the object that called the function.

arrow function, this represents the Header object no matter who called the function

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

Some of the Hooks

A
useState
useEffect
useContext
useRef
useReducer
useMemo
useCallback
useImperativeHandle
useLayoutEffect
useDebugValue
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

useState

A

to track state in a function component.

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

useEffect

A

to perform side effects in your components like fetching data, directly updating the DOM, and timers.

use following lifecycle methods within a single function api i.e. useEffect
useEffect runs
1. componentDidMount - when mounted (runs when sec arg is [ ] )
2. componentDitUpdate - when state updates (runs when sec arg is [something] )
3. componentWillUnmount - when component is destroyed (return a func in useEffect callback - i.e. teardown function)

useEffect(( ) => {
  //Runs on every render
});
useEffect(( ) => {
  //Runs only on the first render
}, [ ]);
useEffect(( ) => {
  //Runs on the first render
  //And any time any dependency value changes
}, [prop, state]);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

useContext

A

makes use of context api. to manage state globally.
share data without passing props, with entire component tree.

createContext(obj);
Context Provider(to supply state value to child Component, that way all components in that component tree will have access to the user Context.)
Use the useContext Hook(access the user Context in all/any components)

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

useRef

A

creates a mutable object to persist values between renders.
to store a mutable value that does not cause a re-render when updated.
to access a DOM element directly.

  1. Does Not Cause Re-renders
    useRef() only returns an Object called current.
    access ref by someobj.current.
  2. Accessing DOM Elements
    add a ref attribute to an HTML element to access it directly in the DOM.
    access ref by inputElement.current.focus();
  3. Tracking State Changes
    to keep track of previous state values. because we are able to persist useRef values between renders.

Also forwardRef() - to call the reference element to be used by consumers of reference component

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

useReducer

A

It allows for custom state logic.

returns an array of two values such as reactive data(to show in UI) and dispatch.
const [ reactive data, dispatch ] = useReducer( reducer , initialState )
works similar to setState.
instead of a function to update the state, here it dispatch(es) an action.

dispatch({ type: "COMPLETE", id: todo.id });
action is of an object that has type:string, payload(optional):any
action triggers reducer function
this function can be passed as first argument into useReducer, second is initial state like 0.
then this func takes state and action as props and switch method is used to update state.
const reducer = (state, action) => {
  switch (action.some_string) { case ...  }
}

eg. use this as a redux pattern to manage state complexities
useReducer use cases
1. Manage multiple states: modify an array
2. Modify complex states, such as arrays or objects: login form

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

useMemo

A

returns a memoized value.
only runs when one of its dependencies update.
This can improve performance.
used to keep expensive, resource intensive functions from needlessly running.

optimize computation cost for improved performance.
memoization - cache result of function call

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

useCallback

A

returns a memoized callback function.
to isolate resource intensive functions so that they will not automatically run on every render.
only runs when one of its dependencies update.
This can improve performance.
to prevent a component from re-rendering unless its props have changed.

eg. when a function is passed to multiple child components.
Wrap the function with useCallback(), this prevents unnecessary re-renders of children component

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

useCallback vs useMemo

A

The useCallback and useMemo Hooks are similar. The main difference is that useMemo returns a memoized value and useCallback returns a memoized function.

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

useImperativeHandle : *rare

A

used to change behavior of exposed ref.

eg. modify the method on native element.

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

useLayoutEffect: *rare

A

like useEffect
runs after render but before painting to screen
blocks visual updates until callback is finished

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

useDebugValue

A

used to add Custom labels for Components.

Is of an argument to be shown in hooks inside React Dev Tools

17
Q

Lazy loading

A

means that a component or a part of code must get loaded when it is required. It is also referred to as code splitting and data fetching . Talking about React specifically, it bundles the complete code and deploys all of it at the same time.