Deck 3 Flashcards

1
Q

What is the lifecycle in React?

A

The React lifecycle refers to the series of methods that are invoked at different stages of a component’s existence: mounting, updating, and unmounting. These methods include componentDidMount, componentDidUpdate, and componentWillUnmount.

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

What is a Higher-Order Component?

A

A Higher-Order Component (HOC) is a function that takes a component and returns a new component with additional props or behavior. It’s used to reuse component logic, such as handling authentication or data fetching.

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

What is the difference between useMemo and React.memo?

A

useMemo is a React hook that memoizes the result of a computation, while React.memo is a higher-order component that memoizes the entire component. useMemo optimizes specific calculations within a component, while React.memo prevents unnecessary re-renders of the entire component.

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

What is a memory leak?

A

: A memory leak occurs when a program allocates memory but fails to release it after it’s no longer needed, leading to reduced performance over time. In React, this can happen if event listeners or intervals are not properly cleaned up.

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

What is the dynamic index in TypeScript?

A

A dynamic index in TypeScript allows you to define an index signature in an object type, specifying that an object can have properties with keys of a certain type and values of a certain type, such as [key: string]: number.

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

What is the grid system in CSS?

A

The grid system in CSS is a layout structure that divides a web page into rows and columns, allowing for precise control over the placement and alignment of elements. CSS Grid Layout and Flexbox are common techniques used.

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

What is lifting state up in React?

A

Lifting state up in React refers to moving state from child components to a common parent component to manage shared state. This is done to allow sibling components to access and update the shared state.

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

What is a custom hook in React.js?

A

A custom hook is a JavaScript function that uses one or more built-in React hooks (like useState or useEffect) to encapsulate and reuse logic across multiple components. It helps keep components clean and reusable

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

What is the difference between null and undefined? ❓

A

null is an assigned value representing the intentional absence of any object value, while undefined means a variable has been declared but not yet assigned a value. Both are falsy values but used in different contexts.

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

What is the output of [] == []?

A

: The output of [] == [] is false because in JavaScript, objects and arrays are compared by reference, not by value. Two different empty arrays are different objects in memory, so the comparison returns false.

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

What are Primitive Types and Union Types?

A

Primitive types in TypeScript are the basic data types such as string, number, boolean, etc. Union types allow a variable to hold more than one type, e.g., string | number, meaning the variable can be a string or a number.

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

How does JavaScript handle memory?

A

JavaScript handles memory automatically through a process called garbage collection, where the JavaScript engine frees up memory by removing objects that are no longer reachable or needed in the code.

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

What are React hooks?

A

React Hooks are functions that let you use state, lifecycle methods, and other React features in functional components. Examples include useState, useEffect, useContext, useReducer, etc.

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

How does the Virtual DOM work?

A

The Virtual DOM is a lightweight copy of the real DOM that React uses to efficiently update the UI. React compares the virtual DOM with the real DOM (reconciliation) and only makes necessary updates, improving performance.

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

How does the Event Loop work in JavaScript?

A

The Event Loop is a mechanism in JavaScript that handles asynchronous operations. It continuously checks the call stack and the task queue, processing events and executing callbacks from the queue when the stack is empty.

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

What are the ways to manage state in a React application?

A

State in a React application can be managed using useState for local state, useReducer for complex state, Context API for global state, and external libraries like Redux or MobX for more complex state management needs.

17
Q

What are the two algorithms that React is built on?

A

React is built on the “Reconciliation” algorithm, which efficiently updates the DOM, and the “Fiber” architecture, which improves rendering performance by breaking rendering work into chunks and allowing for updates to be interrupted.

18
Q

What are the different ways to fetch data in React JS?

A

A:

fetch() API: The native browser method to make network requests. You can use it with promises to handle asynchronous data fetching.

Axios: A popular library for making HTTP requests. It provides a more powerful and flexible API than fetch(), with features like request/response interceptors and automatic JSON parsing.

React Query: A data-fetching library that simplifies fetching, caching, and updating server data in React applications. It helps manage server state and reduces the need for manual data management.

SWR (Stale-While-Revalidate): A React Hooks library for data fetching that provides a simple and efficient way to fetch, cache, and revalidate data.

GraphQL with Apollo Client: A library for using GraphQL in React applications. It allows you to query your API and get only the data you need, making it efficient for complex data fetching.

useEffect Hook with Fetching Logic: You can use the useEffect hook to fetch data when a component mounts or when certain dependencies change, ensuring data is fetched at the right time.

Custom Hooks: You can create custom hooks that encapsulate data-fetching logic, making it reusable across multiple components.

19
Q

Different Ways To Fetch Data in React JS

A

fetch() API

Native browser method.
Works with promises.
2. Axios

Library for HTTP requests.
More powerful than fetch().
3. React Query

Manages fetching, caching, updating server data.
Simplifies server state management.
4. SWR (Stale-While-Revalidate)

React Hooks library for data fetching.
Efficient for fetching, caching, revalidating data.
5. GraphQL with Apollo Client

For querying APIs with GraphQL.
Fetches only the needed data.
6. useEffect Hook with Fetching Logic

Fetch data when component mounts or dependencies change.
7. Custom Hooks

Encapsulate fetching logic.
Reusable across components.

20
Q

You have 10 minutes to explain all 5 SOLID principles to me.

A

Single Responsibility Principle (SRP)

Definition: A class should have only one reason to change, meaning it should have only one job or responsibility.
Example: A class handling user authentication should not also manage user profiles.
2. Open/Closed Principle (OCP)

Definition: Software entities (classes, modules, functions, etc.) should be open for extension but closed for modification.
Example: Adding new features should be done by extending existing classes, not modifying them.
3. Liskov Substitution Principle (LSP)

Definition: Subtypes should be substitutable for their base types without altering the correctness of the program.
Example: If a function expects an object of a parent class, it should work correctly if an object of a derived class is passed.
4. Interface Segregation Principle (ISP)

Definition: Clients should not be forced to depend on interfaces they do not use.
Example: Instead of one large interface, create multiple smaller, specific ones so that implementing classes only need to fulfill relevant contracts.
5. Dependency Inversion Principle (DIP)

Definition: High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions.
Example: A high-level class (like a service) should depend on an interface (abstraction) rather than a concrete implementation (detail), allowing for easier swapping of implementations.