React Flashcards

1
Q

Explain useMemo

A

It allows us to “remember” a computed value between renders, thus reducing the amount of work that needs to be done in a given render.
Use cases: slow/expensive functions and preserved references

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

Explain refs in React

A

When you want a component to “remember” some information, but you don’t want that information to trigger new renders, you can use a ref.
You can access the current value of that ref through the ref.current property. This value is intentionally mutable, meaning you can both read and write to it.
Like state, refs are retained by React between re-renders. However, setting state re-renders a component. Changing a ref does not!
When a piece of information is used for rendering, keep it in state. When a piece of information is only needed by event handlers and changing it doesn’t require a re-render, using a ref may be more efficient.

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

What is debouncing?

A

Debouncing lets you delay some action until the user “stops doing things”.

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

Explain how you can use useRef to manipulate the DOM.

A

The useRef Hook returns an object with a single property called current. Initially, myRef.current will be null. When React creates a DOM node for this <div>, React will put a reference to this node into myRef.current. You can then access this DOM node from your event handlers and use the built-in browser APIs defined on it.
ex.
const myRef = useRef(null);
<div ref={myRef}>
myRef.current.scrollIntoView();

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

Explain forwardRefs.

A

React does not let a component access the DOM nodes of other components (even its own children). To do so, you have to specify that the component is forwarding a ref to its children.

In design systems, it is a common pattern for low-level components like buttons, inputs, and so on, to forward their refs to their DOM nodes.

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

What is an escape hatch in React?

A

Escape hatches let you “step outside” React and connect to external systems.

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

What does it mean for a component to be mounted in React?

A

It has a corresponding element created in the DOM and is connected to that.

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

What’s the difference between a Controlled component and an Uncontrolled one in React?

A

A Controlled Component is one that takes its current value through props and notifies changes through callbacks like onChange. A parent component “controls” it by handling the callback and managing its own state and passing the new values as props to the controlled component. You could also call this a “dumb component”.
A Uncontrolled Component is one that stores its own state internally, and you query the DOM using a ref to find its current value when you need it. This is a bit more like traditional HTML.

In a controlled component, form data is handled by a React component. The alternative is uncontrolled components, where form data is handled by the DOM itself.
To write an uncontrolled component, instead of writing an event handler for every state update, you can use a ref to get form values from the DOM.

// Controlled:
<input type=”text” value={value} onChange={handleChange} />

// Uncontrolled:
<input type=”text” defaultValue=”foo” ref={inputRef} />
// Use inputRef.current.value to read the current value of <input></input>

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

What is reconciliation?

A

When a component’s props or state change, React decides whether an actual DOM update is necessary by comparing the newly returned element with the previously rendered one. When they are not equal, React will update the DOM. This process is called reconciliation.

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

What is the difference between Component and Container in Redux?

A

Component is part of the React API. A Component is a class or function that describes part of a React UI.
Container is an informal term for a React component that is connected to a redux store. Containers receive Redux state updates and dispatch actions, and they usually don’t render DOM elements; they delegate rendering to presentational child components.

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

What is the purpose of using super constructor with props argument in React?

A

A child class constructor cannot make use of this reference until super() method has been called. The same applies for ES6 sub-classes as well. The main reason of passing props parameter to super() call is to access this.props in your child constructors.

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

Name the different lifecycle methods in class components

A

componentWillMount - this is most commonly used for App configuration in your root component.

componentDidMount - here you want to do all the setup you couldn’t do without a DOM, and start getting all the data you need. Also if you want to set up eventListeners etc. this lifecycle hook is a good place to do that.

componentWillReceiveProps - this lifecyclye acts on particular prop changes to trigger state transitions.

shouldComponentUpdate - if you’re worried about wasted renders shouldComponentUpdate is a great place to improve performance as it allows you to prevent a rerender if component receives new prop. shouldComponentUpdate should always return a boolean and based on what this is will determine if the component is rerendered or not.

componentWillUpdate - rarely used. It can be used instead of componentWillReceiveProps on a component that also has shouldComponentUpdate (but no access to previous props).

componentDidUpdate - also commonly used to update the DOM in response to prop or state changes.

componentWillUnmount - here you can cancel any outgoing network requests, or remove all event listeners
associated with the component.

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

What is {this.props.children} and when you should use it?

A

You can use props.children on components that represent ‘generic boxes’ and that don’t know their children ahead of time. It is used to display whatever you include between the opening and closing tags when invoking a component.

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

What’s the typical flow of data like in a React + Redux app?

A

Callback from UI component dispatches an action with a payload, which then is intercepted in a reducer, possibly producing a new application state, which is then propagated down through the tree of components in the application from the Redux store.

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

What are the different phases of ReactJS component lifecycle?

A
  1. Initialization: In this phase react component prepares setting up the initial state and default props.
  2. Mounting: The react component is ready to mount in the browser DOM. This phase covers componentWillMount and componentDidMount lifecycle methods.
  3. Updating: In this phase, the component get updated in two ways, sending the new props and updating the state. This phase covers shouldComponentUpdate, componentWillUpdate and componentDidUpdate lifecycle methods.
  4. Unmounting: In this last phase, the component is not needed and get unmounted from the browser DOM. This phase include componentWillUnmount lifecycle method.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why do class methods need to be bound to a class instance?

A

In JavaScript, the value of this changes depending on the current context. Within React class component methods, developers normally expect this to refer to the current instance of a component, so it is necessary to bind these methods to the instance.

17
Q

Explain the flux pattern

A

Action ➡️ Dispatcher ➡️ Store ➡️ View
In the Flux pattern, the Store is the central authority for all data; any mutations to the data must occur within the
store. Changes to the Store data are subsequently broadcast to subscribing Views via events. Views then update
themselves based on the new state of received data.
To request changes to any Store data, Actions may be fired. These Actions are controlled by a central Dispatcher;
Actions may not occur simultaneously, ensuring that a Store only mutates data once per Action.
The strict unidirectional flow of this Flux pattern enforces data stability, reducing data-related runtime errors
throughout an application.

18
Q

Explain React portals.

A

createPortal lets you render some children into a different part of the DOM.
Portals allow developers to render their elements outside the React hierarchy tree without comprising the parent-child relationship between components.
Portals are great for places where you want to render elements on top of each other, such as modals, loading screens, and cookie alerts.

19
Q

What is flushSync?

A

flushSync lets you force React to flush any updates inside the provided callback synchronously. This ensures that the DOM is updated immediately.

20
Q

Explain the Single Responsibility Principle.

A

In React, the Single Responsibility Principle means that a component is required to have only one reason to change. In short, the Single Responsibility Principle means that code with the same functionality should not exist in multiple places. Component Composition makes SRP easier to maintain so that there can be a single source of truth.
Writing big components with many responsibilities increases technical debt and makes it hard to modify existing or create new functionality.