React Flashcards

1
Q

Hooks

A

Hooks are a new addition in React 16.8. They let you use state and other React features without writing a class.

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

Built in react hooks

A

Basic Hooks
useState
useEffect
useContext

Additional Hooks
useReducer
useCallback
useMemo
useRef
useImperativeHandle
useLayoutEffect
useDebugValue
useDeferredValue
useTransition
useId

Library Hooks
useSyncExternalStore
useInsertionEffect

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

useRef

A
useRef
const refContainer = useRef(initialValue);

useRef returns a mutable ref object whose .current property is initialized to the passed argument (initialValue). The returned object will persist for the full lifetime of the component.

A common use case is to access a child imperatively:

function TextInputWithFocusButton() {
  const inputEl = useRef(null);
  const onButtonClick = () => {
    // `current` points to the mounted text input element
    inputEl.current.focus();
  };
  return (
    <>
  Focus the input
>   ); }

Essentially, useRef is like a “box” that can hold a mutable value in its .current property.

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

Higher-Order Components

A

A higher-order component (HOC) is an advanced technique in React for reusing component logic. HOCs are not part of the React API, per se. They are a pattern that emerges from React’s compositional nature.

Concretely, a higher-order component is a function that takes a component and returns a new component.

E.g
const EnhancedComponent = higherOrderComponent(WrappedComponent);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Inheritance in React

A

At Facebook, we use React in thousands of components, and we haven’t found any use cases where we would recommend creating component inheritance hierarchies.

Props and composition give you all the flexibility you need to customize a component’s look and behavior in an explicit and safe way. Remember that components may accept arbitrary props, including primitive values, React elements, or functions.

If you want to reuse non-UI functionality between components, we suggest extracting it into a separate JavaScript module. The components may import it and use that function, object, or a class, without extending it.

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

Render props

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

Lifting state up

A

Often, several components need to reflect the same changing data. We recommend lifting the shared state up to their closest common ancestor. Let’s see how this works in action.

Lifting up the State: As we know, every component in React has its own state. Because of this sometimes data can be redundant and inconsistent. So, by Lifting up the state we make the state of the parent component a single source of truth and pass the data of the parent to its children.

Time to use Lift up the State: If the data in “parent and children components” or in “cousin components” is Not in Sync.

Example 1: If we have 2 components in our App. A -> B where, A is parent of B. keeping the same data in both Component A and B might cause inconsistency of data.

Example 2: If we have 3 components in our App.

    A
   / \
  B   C Where A is the parent of B and C. In this case, If there is some Data only in component B but, component C also wants that data. We know Component C cannot access the data because a component can talk only to its parent or child (Not cousins).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly