React Flashcards

1
Q

How do you create refs in React

A

You can create refs in React using React.createRef(). Refs provide a way to access the DOM directly or interact with a child component, you can access its underlying DOM node or React instance by referencing the current property of the ref

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

How often does the React useState update? Why?

A

The useState hook in React updates asynchronously, meaning it does not update the state immediately. React batches multiple state updates to optimise performance and minimise unnecessary re-renders. This helps React handle large-scale applications efficiently. The state updates occur on the next render, ensuring consistency across the application.

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

Name 3 advantages of using React Hooks

A

Improve Readability: Hooks make components easier to read and maintain by reducing complexity.
Reusability: Hooks allow sharing stateful logic between components without having to deal with higher-order components or render props
Side-Effect Management: Hooks like useEffect allow for clean and controlled side-effect handling in functional components

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

Name 2 advantages of using React.js

A

Component-based architecture: React allows developers to build encapsulated components that manage their own state, making the UI more reusable and easier to maintain.
Virtual DOM: React uses a virtual DOM to optimise rendering performance by minimising direct manipulation of the real DOM, leading to faster updates.

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

Outline the different stages of the React.js lifecycle

A
  1. Mounting: This is when the component is being inserted into the DOM
  2. Updating: This happens when the component’s state or props change, causing a re-render
  3. Unmounting: This occurs when the component is being removed from the DOM
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What happens if you attempt to update the state directly?

A

If you attempt to update the state directly, React will not detect the change and will not re-render the component. State should only be updated using setState() (in class components) or the useState setter function (functional components)

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

Explain whether Hooks replace higher-order components

A

Hooks don’t replace higher-order components (HOCs), but they provide an alternative for sharing logic between components. Hooks like defect and useState allow functional components to manage state and side effects without needing HOCs or render props, which can make the component tree cleaner and easier to follow

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

Which method would you use to handle events in React?

A

In React, events are handling by passing functions as event handlers in JSX. The event handler methods are named using camelCase (e.g. onClick, onSubmit), and a function is passed to handle the event instead of a string

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

Which method would you use to add attributes to components conditionally?

A

You can add attributes conditionally using JavaScript expressions in JSX. For example, you can use ternary operators or logical && to conditionally add classes or attributes

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

In which situation would you use useMemo() in React?

A

When you want to memorise expensive computations and avoid recalculating them on every render. useMemo() caches the result of a function and only recomputes it if the dependencies change, thus improving performance by reducing unnecessary renders.

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

Explain what useState is

A

useState is a hook in React that allows functional components to have local state. It returns a state variable and a setter function to update the state.

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

Explain what an event is in React

A

An event in React is an interaction triggered by user input, such as clicks, typing, or scrolling.

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

What is a component?

A

A component is a building block in React used to create UIs. It takes inputs (props) and returns a React element to at describes how a section of the UI should appear.

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

What is the difference between class and functional components?

A

Class components are stateful and can use lifecycle methods, while functional components are stateless by default but can use hooks (like useState and useEffect) to manage state and lifecycle events. Functional components are generally simpler and have better performance when hooks are used.

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

Why are function-based components preferred over class-based components in modern React development?

A

Function-based components have become more popular because they are more concise and easier to write compared to class-based components. They also support React hooks, which allow you to use state and other React features without the need for classes. Although older React project might still use class components, function-based components are now standard for new development.

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

Why is JSX in React, and how does it differ from regular JavaScript?

A

JSX stands for JavaScript XML, and it is a syntax extension that looks like HTML but is actually written in JavaScript. Under the hood, JSX gets compiled down to plain JavaScript. This allows developers to write more readable code that resembles HTML but it’s actually transformed into React elements.

17
Q

What is the Virtual DOM, and how does it improve React’s performance?

A

The virtual DOM is a lightweight, in-memory representation of the actual DOM. When a component’s state or data changes, React updates the Virtual DOM first, comparing the current and previous versions to identify the specific nodes that need to be updated. Only those nodes are then updated in the actual DOM, improving the performance of the application by minimising direct DOM manipulation.

18
Q

What is the role of React DOM in a React application?

A

React DOM is a companion library that is used to update and render the components in the actual DOM. In a React application, React DOM renders the component tree by taking the virtual DOM and updating the real DOM with only the changes that are necessary. In a typical React project, React DOM is imported and used in the index.js file to render the root component into the actual DOM

19
Q

What additional tools are typically used with React to build modern applications?

A

Since React only focuses on building user interfaces, developers typically use other tools for additionally concerns such as routing (e.g., React Router), making HTTP calls (Axios or fetch), managing application state (Redux or Context API), internationalisation, form validation, and animations. React’s flexibility allows developers to choose the tools that best fit their project’s needs.

20
Q

What is the purpose of fragments in React?

A

Fragments allow React components to return multiple elements without adding extra nodes to the DOM. They are represented by empty angle brackets

21
Q

What is the state hook in React?

A

The state hook (useState) allows you to add state to function components, enabling components to store and manage data that changes over time.

22
Q

How should props be treated in React?

A

Props should be treated as immutable, meaning they are read-only and should not be modified directly.

23
Q

Why is React considered to follow the separation of concerns principle?

A

React allows developers to divide functionality into distinct components, which encapsulates their logic and styles. This modular approach adheres to the separation of concerns principle.

24
Q

What is a pure function in react?

A

A pure function is a function that returns the same result given the same input and does not modify external object or state.

25
Q

What is the purpose of the effect hook (useEffect)

A

The useEffect hook is used to perform side effects, such as data fetching or updating the DOM. It can also include a clean-up function to handle unmounting or dependency changes.

26
Q

How does React handle HTTP requests and responses?

A

React can send HTTP requests using libraries like axios. The requests are sent via the useEffect hook, and errors can be handled with try-catch blocks or .catch() methods in promises.