React Flashcards

1
Q

What is React and why is it popular?

A

React is a JavaScript library developed by Facebook for building user interfaces, especially for single-page applications. It’s popular because of its component-based architecture, efficient rendering using the virtual DOM, and flexibility to integrate with various backends.

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

Explain the virtual DOM and its significance in React.

A

The virtual DOM is an in-memory representation of the real DOM. Instead of making direct changes to the actual DOM, React updates the virtual DOM. This allows React to determine the most efficient way to make changes to the actual DOM by comparing the current virtual DOM with the next version, a process called “reconciliation.” This results in faster and more efficient updates.

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

What is JSX in React? How is it different from HTML?

A

JSX (JavaScript XML) is a syntax extension for JavaScript used in React to describe UI components. While it resembles HTML, it allows for the embedding of JavaScript expressions. JSX produces React elements, whereas HTML produces browser DOM elements. In JSX, properties and methods are camelCased rather than lowercased as in HTML.

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

How do you create a React component?

A

A React component can be created using a class or a function. The component returns a React element, typically written in JSX, which defines how the component should appear. Each component should also include a ‘render’ method when using class components.

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

What is the purpose of the render method in a React component?

A

The render method specifies the UI output of a React component. It returns a single React element (either a native DOM element or another component) that represents the component’s visual representation.

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

What is the significance of state in React?

A

State in React represents the data that can change over time within a component. It determines the component’s behavior and rendering. Whenever state changes, the component re-renders to reflect those changes.

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

What is a functional component in React?

A

A functional component is a simpler way to write a React component. It’s just a JavaScript function that returns a React element. Unlike class components, functional components don’t have a ‘render’ method and don’t manage their own state or lifecycle methods without hooks.

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

How do you pass data from a parent component to a child component?

A

Data is passed from a parent component to a child component through “props”. The parent component defines the props for the child component, and the child accesses them as arguments to its function or through ‘this.props’ in class components.

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

Explain the concept of props in React.

A

Props (short for “properties”) are a way of passing data from parent to child components in React. They are read-only and help to make components reusable by giving components the ability to receive data from its parent component.

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

What is React’s key principle for optimizing performance?

A

React’s key principle for optimizing performance is its use of the virtual DOM and the reconciliation process. By updating only what’s necessary in the real DOM, based on differences between the current virtual DOM and the next version, React ensures efficient and optimal rendering.

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

What are controlled components in React?

A

Controlled components in React are form elements like input, textarea, and select where the value of the element is controlled by the React state. This means the element’s value is set by the component’s state and can be modified by event handlers.

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

What is the purpose of the useState hook in React?

A

The useState hook allows functional components to use state. It returns an array where the first element is the current state value, and the second element is a function to update that state. It provides a way to add state management functionality to functional components without converting them to class components.

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

How do you handle events in React?

A

In React, events are handled using event handlers, which are JavaScript functions. These handlers can be passed as props to child components or used directly in components. React’s event system is synthetic, meaning it wraps around the browser’s native event system, providing a consistent API across browsers.

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

Describe the component lifecycle methods in React.

A

In React, class components have lifecycle methods that get executed at various stages of a component’s life, such as mounting, updating, and unmounting. Key methods include: componentDidMount, shouldComponentUpdate, componentDidUpdate, and componentWillUnmount.

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

Explain the difference between componentDidMount and useEffect.

A

componentDidMount is a lifecycle method in class components that runs after the component output is rendered to the DOM. useEffect is a hook in functional components that can mimic the behavior of various lifecycle methods, including componentDidMount, depending on its dependency array. If the dependency array is empty, useEffect acts like `componentDidMount

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

What is Redux, and how does it work with React?

A

Redux is a state management library often used with React. It provides a centralized store for state that every component can access. React components interact with the store by dispatching actions and subscribing to state changes. Redux ensures consistent state management with principles like immutability and pure reducer functions.

17
Q

What is routing in React, and how can you implement it?

A

Routing in React allows navigation between different components based on the URL path. React Router is a popular library for implementing routing in React apps. With React Router, you can define Route components that specify which component should be rendered for different paths.

18
Q

What is the significance of keys in React when rendering lists?

A

Keys are special string attributes you need to include when creating lists of elements in React. They help React identify which items have changed, are added, or are removed, optimizing the rendering by reusing existing components.

19
Q

How do you make AJAX requests in React?

A

In React, AJAX requests can be made using browser built-in methods like fetch or third-party libraries like axios. AJAX calls are often placed in lifecycle methods (like componentDidMount in class components) or hooks (like useEffect in functional components) to fetch data after a component is rendered.

20
Q

What is Redux Thunk, and why is it used?

A

Redux Thunk is middleware for Redux that allows you to write action creators that return a function instead of an action object. This is useful for handling asynchronous actions, such as API calls. Thunks can be used to delay the dispatch of an action or to dispatch only under certain conditions.

21
Q

Describe the concept of context in React and how it can be useful.

A

Context in React provides a way to pass data through the component tree without having to pass props manually at every level. It’s useful when you have global data that many components need access to, like themes or user authentication.

22
Q

Explain the purpose of the useReducer hook in React.

A

The useReducer hook in React provides a more structured way to manage state in functional components. It’s often used for state logic that is more complex than what useState can handle. It takes a reducer function and an initial state as arguments and returns the current state and a dispatch method.

23
Q

What are Higher Order Components (HOCs) in React?

A

HOCs are a pattern in React where a function takes a component and returns a new component with additional props or behavior. They are useful for reusing component logic and can be thought of as “component factories.”

24
Q

What is server-side rendering (SSR) in React? Why is it beneficial?

A

SSR means rendering React components on the server rather than the client. This generates a full page HTML for the initial load, improving initial load performance and SEO, as search engines can crawl the fully rendered page.

25
Q

Compare and contrast React’s PureComponent and shouldComponentUpdate.

A

PureComponent performs a shallow comparison on its state and props, re-rendering only when there are actual changes. shouldComponentUpdate is a lifecycle method that allows you to manually determine whether a component should re-render by comparing the current props and state to the next ones. While both aim to optimize rendering, PureComponent provides an automatic check, whereas shouldComponentUpdate requires custom logic.

26
Q

What is the Virtual DOM reconciliation process in React?

A

The reconciliation process is how React updates the actual DOM by comparing changes between the current virtual DOM and the next version. It calculates the most efficient way to update the DOM, making only the necessary changes.

27
Q

Explain the concept of code splitting in React.

A

Code splitting is a feature that lets you split your code into smaller chunks, which can be loaded on demand. This improves the performance of your app by reducing the initial load time. With tools like React.lazy and Suspense, you can easily implement code splitting in your React app.

28
Q

What are React hooks, and why were they introduced?

A

React hooks are functions that let you use state and other React features in functional components. Introduced in React 16.8, hooks were designed to make it easier to reuse stateful logic between components, simplify complex class component logic, and enable better integration with the React ecosystem.

29
Q

Discuss the challenges and solutions related to performance optimization in React applications.

A

Challenges in optimizing React apps include unnecessary re-renders, large bundles, and inefficient state updates. Solutions include using PureComponent, memoization, code splitting, efficient event handling, and optimizing state updates with tools like Redux or the Context API.

30
Q

How does React Fiber work, and what improvements does it bring to React?

A

React Fiber is a reimplementation of React’s core reconciliation algorithm. It introduces features like incremental rendering, allowing React to split rendering work into chunks and pause/resume according to priority. It improves the responsiveness and perceived performance of complex React applications by prioritizing user interactions over background tasks.