\
Basic What is ReactJS?React JS is a JavaScript library for building user interfaces, primarily used for creating single-page applications where data can change without reloading the page.
\
Why ReactJS is Used?We need React to efficiently build dynamic and interactive user interfaces with reusable components, which improves development speed and maintainability.
\
How Does ReactJS work?ReactJS works by using a component-based architecture to build UIs, where each component manages its own state and behavior. React uses a virtual DOM to efficiently update the actual DOM by only rendering the components that change. When the state or props of a component change, React compares the virtual DOM with the previous one, finds the differences, and updates the DOM efficiently. This approach ensures better performance and a smooth user experience.
\
What are the features/advantage of ReactJS?\
What is virtual DOM in React?The Virtual DOM is a lightweight, in-memory representation of the real DOM in React. When a component’s state or props change, React updates the Virtual DOM instead of the real DOM immediately. It then compares the updated Virtual DOM with a snapshot of the previous one (a process called “diffing”) and identifies the minimum number of changes required. React then updates only the parts of the real DOM that have changed, improving performance by minimizing direct DOM manipulations.
\
Differentiate between real DOM and virtual DOM?\
What is JSX?JSX is a syntax extension for JavaScript that allows you to write HTML-like code within JavaScript, making it easier to visualize the UI structure.
\
How do browsers read JSX?Browsers do not natively understand JSX, as it is not a standard JavaScript syntax. JSX is a syntax extension for JavaScript that looks similar to HTML but is meant for React. Here's how browsers handle it:
\
JSX Transpilation: JSX is first transpiled (translated) into regular JavaScript using tools like Babel. During this process, JSX elements are converted into React.createElement() calls.
\
React.createElement(): Once transpiled, JSX is represented as JavaScript objects describing the DOM tree. These objects are then processed by React, which renders them into actual DOM elements.
For example, JSX like:
const element =is transpiled to:
const element = React.createElement('h1', null, 'Hello, world!');\
\
What are components in React?In React, components are the building blocks used to create user interfaces. They encapsulate parts of the UI and allow you to split the UI into independent, reusable pieces.
\
How to create components in ReactJS?1. Functional Components:
Functional components are the simplest form of components in React. They are JavaScript functions that return JSX (React’s syntax to write HTML-like code).
Example:
function MyComponent() { return (\ 2. Class Components:
Class components are ES6 classes that extend React.Component. They can also hold and manage state and lifecycle methods.
Example:
import React, { Component } from 'react'; class MyComponent extends Component { render() { return (\
What are the lifecycle Methods in Class ComponentsLifecycle methods in class components are hooks that allow you to execute code at specific points during a component's lifecycle, including:
\
Explain the difference between functional and class components in React.\
How do you pass data from parent to child?Data is passed from parent to child components using props.
\
Explain props and state in React with differences.Key Differences:
\
What is the children prop in React?The children prop in React is a special prop that allows components to pass other elements or components as its content. It is used to display whatever is placed between the opening and closing tags of a component.
\
What is defaultProps in React?defaultProps in React is a way to define default values for props in a component. If a prop is not provided by the parent component, the component will use the value specified in defaultProps.
\
What is prop drilling and how to avoid it?Prop drilling is basically a situation when the same data is being sent at almost every level due to requirements in the final level. The problem with Prop Drilling is that whenever data from the Parent component will be needed, it would have to come from each level, Regardless of the fact that it is not needed there and simply needed in last.
\
What is the Purpose of Pure Components?Pure Components in React optimize performance by implementing a shallow comparison on props and state, re-rendering only when there are changes, thus preventing unnecessary renders.
\n How to Create Elements in a Loop in React?You can create elements in a loop in React by using JavaScript’s array methods, such as map().
\
What is a key in React?Keys are very important in lists for the following reasons:
\
Why are indexes for keys not recommended?Keys are unique identifiers used in lists to help React identify which items have changed, been added, or removed. They are important for efficient re-rendering and to maintain the correct order of elements.
\
Explain one-way data binding in React?One-way data binding in React means that data flows in a single direction, from parent components to child components through props, ensuring predictable and controlled updates to the UI.
\
What are hooks in React?Hooks in React are special functions that allow developers to use state and other React features (such as lifecycle methods)
\
Explain the useState hook in React?The most used hook in React is the useState() hook. It allows functional components to manipulate DOM elements before each render. Using this hook we can declare a state variable inside a function but only one state variable can be declared using a single useState() hook. Whenever the useState() hook is used, the value of the state variable is changed and the new variable is stored in a new cell in the stack.
\
What is useEffect() hook and how to manage side effects?The useEffect() hook in React eliminates the side effect of using class based components. It is used as an alternative to componentDidUpdate() method. The useEffect() hook accepts two arguments where second argument is optional.
\
What are custom hooks in React?A custom hook is a function that encapsulates reusable logic using React hooks. It always starts with “use” to follow the convention.
\
What is conditional rendering in React?Conditional Rendering: In React, it means rendering different UI elements based on certain conditions, often using JavaScript operators like `if`, `ternary`, or `&&
\
What is a Fragment?A Fragment is a wrapper used in React to group multiple elements without adding extra nodes to the DOM.
\
What is React Developer Tools?React Developer Tools is a Chrome DevTools extension for the React JavaScript library. A very useful tool, if you are working on React.js applications. This extension adds React debugging tools to the Chrome Developer Tools. It helps you to inspect and edit the React component tree that builds the page, and for each component, one can check the props, the state, hooks, etc.
\
How to use styles in ReactJS?CSS modules are a way to locally scope the content of your CSS file. We can create a CSS module file by naming our CSS file as App.modules.css and then it can be imported inside App.js file using the special syntax mentioned below.
Syntax:
import styles from './App.module.css';\
What is react-redux?Redux is a predictable state container for JavaScript applications. It helps you write apps that behave consistently, run in different environments (client, server, and native), and are easy to test. Redux manages an application’s state with a single global object called Store.
\
What are benefits of using react-redux?Redux solves the state transfer problem by storing all of the states in a single place called a store. So, managing and transferring states becomes easier as all the states are stored in the same convenient store. Every component in the application can then directly access the required state from that store.
\
Explain the core components of react-redux.The state of your whole application is stored in an object tree within a single-store.
A single state tree makes it easier to debug or inspect an application \n It gives you a faster development cycle by enabling you to persist in your app's navigation state
\
The only way to change the state is to initiate an action, an object describing what happened.
This feature ensures that no events like network callbacks or views can change the state. They can only express an intent to change the state \n Actions are just plain objects; they can be logged, serialized, stored, and later replayed for debugging or testing purposes
\
To specify how actions transform the state tree, you need to write pure reducers.
The user can return new state objects instead of mutating the previous stateThe user can start with a single reducer, and, as the app grows, can split it off into smaller reducers that manage specific parts of the state treeBecause reducers are just functions, it’s easy to control the order in which they are called, pass additional data, or even make reusable reducers
\
What is context?Context is a React feature that allows you to share state and other values between components without having to pass props manually at every level.
\
What is context API?Context API is used to pass global variables anywhere in the code. It helps when there is a need for sharing state between a lot of nested components. It is light in weight and easier to use, to create a context just need to call React.createContext(). It eliminates the need to install other dependencies or third-party libraries like redux for state management. It has two properties Provider and Consumer.
\
Explain provider and consumer in ContextAPI?A provider is used to provide context to the whole application whereas a consumer consume the context provided by nearest provider. In other words The Provider acts as a parent it passes the state to its children whereas the Consumer uses the state that has been passed.
\
How to handle buttons in React?Handling buttons in React is a straightforward process. Typically, buttons are used to trigger events, perform actions, or update state when clicked.
\
You clicked {count} times
{/* onClick event */}\
\
Preventing Default Behavior
If a button has default behavior (like a form submission), you can prevent it using event.preventDefault().
\ 4. Handling Multiple Buttons If you have multiple buttons, you can use the id or other identifying attributes to differentiate their actions.
function MultipleButtons() { const handleButtonClick = (action) => { console.log(${action} button clicked); }; return (\
How to handle inputs in React?In React, inputs are handled using controlled components, where the input's value is tied to the component's state through the value attribute, and updated using the onChange event. This allows for efficient management of inputs like text fields, checkboxes, and dropdowns.
const [inputValue, setInputValue] = useState(''); setInputValue(e.target.value)} />\
What is React Router?React Router is a library that enables dynamic routing in React applications, allowing you to navigate between different components or pages without reloading the entire application.
\
Explain the components of React Router.React Router is a popular library for handling routing in React applications, allowing you to manage navigation between different components or pages. The main components of React Router include:
\ 1. BrowserRouter
\ 2. Routes and Route
\ 3. Link
\ 4. useNavigate
\ 5. useParams
\ 6. useLocation
\ 7. Navigate
\ 8. Outlet
\
How does Routing work in ReactJS?Routing in React is typically managed by libraries like React Router. It works by mapping URL paths to specific components. When a user navigates to a URL, the router displays the corresponding component without reloading the page, creating a seamless single-page application experience.
\
How to implement data fetching in ReactJS?Data fetching in React can be implemented using various methods, depending on your needs and the complexity of the application. The most common approach is to use React’s lifecycle methods or hooks like useEffect to fetch data from APIs or other sources.
\ 1. Using useEffect Hook
The useEffect hook allows you to perform side effects in functional components, including data fetching. This is the most common approach when working with functional components in React.
\
import React, { useState, useEffect } from 'react'; function DataFetchingComponent() { const [data, setData] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { // Fetch data when the component mounts fetch('https://api.example.com/data') .then((response) => { if (!response.ok) { throw new Error('Network response was not ok'); } return response.json(); }) .then((data) => { setData(data); setLoading(false); // Data is fetched, stop loading }) .catch((error) => { setError(error.message); setLoading(false); }); }, []); // Empty dependency array ensures it only runs once when component mounts if (loading) returnLoading...
; if (error) returnError: {error}
; return (\ 2. Using axios for Data Fetching
Instead of using the native fetch API, you can use third-party libraries like Axios, which makes handling promises simpler and supports features like request cancellation.
\
import React, { useState, useEffect } from 'react'; import axios from 'axios'; function AxiosDataFetching() { const [data, setData] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { // Fetch data using Axios axios.get('https://api.example.com/data') .then((response) => { setData(response.data); setLoading(false); }) .catch((error) => { setError(error.message); setLoading(false); }); }, []); if (loading) returnLoading...
; if (error) returnError: {error}
; return (\ 3. Using async/await for Cleaner Syntax
You can use async/await inside the useEffect hook for more readable asynchronous code.
\
import React, { useState, useEffect } from 'react'; function AsyncDataFetching() { const [data, setData] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { const fetchData = async () => { try { const response = await fetch('https://api.example.com/data'); if (!response.ok) { throw new Error('Error fetching data'); } const data = await response.json(); setData(data); setLoading(false); } catch (error) { setError(error.message); setLoading(false); } }; fetchData(); }, []); if (loading) returnLoading...
; if (error) returnError: {error}
; return (\
Explain the difference between React and Angular?React is a JavaScript library focused on building user interfaces with a component-based architecture and uses a virtual DOM for efficient updates. It’s flexible and allows you to choose additional libraries for routing and state management.
Angular is a full-fledged JavaScript framework that provides a complete solution for building web applications. It uses TypeScript, follows a model-view-controller (MVC) or model-view-view model (MVVM) architecture, and includes built-in tools for routing, state management, and dependency injection.
\
Create a Reusable Button Component with Props.Button.js
import React from 'react'; import PropTypes from 'prop-types'; import './Button.css'; // Optional: for styling const Button = ({ onClick, type, children, className, disabled }) => { return ( ); }; Button.propTypes = { onClick: PropTypes.func, type: PropTypes.oneOf(['button', 'submit', 'reset']), children: PropTypes.node.isRequired, className: PropTypes.string, disabled: PropTypes.bool, }; Button.defaultProps = { onClick: () => {}, type: 'button', className: '', disabled: false, }; export default Button;\ Button.css
.btn { padding: 10px 20px; font-size: 16px; border: none; border-radius: 4px; cursor: pointer; transition: background-color 0.3s; } .btn:hover { background-color: #ddd; } .btn:disabled { cursor: not-allowed; opacity: 0.5; }\ Usage Example:
import React from 'react'; import Button from './Button'; function App() { const handleClick = () => { alert('Button clicked!'); }; return (\
Create a form, email, pass, user, with validations.Form.js
import React, { useState } from 'react'; const Form = () => { const [formData, setFormData] = useState({ email: '', password: '', username: '' }); const [errors, setErrors] = useState({ email: '', password: '', username: '' }); const [isSubmitted, setIsSubmitted] = useState(false); const handleChange = (e) => { const { name, value } = e.target; setFormData((prev) => ({ ...prev, [name]: value, })); }; const validate = () => { const { email, password, username } = formData; const newErrors = {}; if (!email) { newErrors.email = 'Email is required'; } else if (!/\S+@\S+\.\S+/.test(email)) { newErrors.email = 'Email address is invalid'; } if (!password) { newErrors.password = 'Password is required'; } else if (password.length < 6) { newErrors.password = 'Password must be at least 6 characters long'; } if (!username) { newErrors.username = 'Username is required'; } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleSubmit = (e) => { e.preventDefault(); if (validate()) { setIsSubmitted(true); // Handle form submission (e.g., API call) console.log('Form data submitted:', formData); } }; return ( ); }; export default Form;\ Form.css
.error { color: red; font-size: 0.875em; } .success { color: green; font-size: 1em; } form { max-width: 400px; margin: auto; } div { margin-bottom: 15px; }\ Usage:
import React from 'react'; import Form from './Form'; import './Form.css'; function App() { return (\ \
Intermediate What is the difference between controlled and uncontrolled components in React?Controlled components in React have their form data managed by the component’s state, with updates handled via onChange events. Uncontrolled components manage their form data through the DOM, using refs to access values. Controlled components offer better control and synchronization, while uncontrolled components are simpler but less flexible.
\
Explain the lifecycle methods of componentsA React Component can go through four stages of its life as follows.
Initialization: This is the stage where the component is constructed with the given Props and default state. This is done in the constructor of a Component Class.
Mounting: Mounting is the stage of rendering the JSX returned by the render method itself.
Updating: Updating is the stage when the state of a component is updated and the application is repainted.
Unmounting: As the name suggests Unmounting is the final step of the component lifecycle where the component is removed from the page.
\
useState: Creates state variables that trigger re-renders when updated.
useRef: Creates a mutable reference that persists across renders without causing re-renders, commonly used to access DOM elements directly.
\
StrictMode is a tool for highlighting potential problems in a React application. It activates additional checks and warnings for its descendants, such as identifying unsafe lifecycle methods, detecting side effects in components, and ensuring best practices. StrictMode is a development-only feature and has no impact on production builds.
\
How Do You Modularize Code in a React Project?Modularizing code in a React project involves organizing components, utilities, and styles into separate files and directories, making the codebase more maintainable and scalable. This can be achieved by:
\
How to implement custom hooks in React?Custom hooks in React are functions that start with use and allow you to encapsulate and reuse stateful logic across multiple components. They can call other hooks and return values such as state variables or functions. For example, a custom hook like useCounter can manage counter state and provide methods to increment, decrement, or reset the counter.
\
How does React implement reusability in components?React implements reusability in components through several key features:
\
What is a higher-order component (HOC) in React?A Higher-Order Component (HOC) in React is a function that takes a component and returns a new component with additional props or behavior. It is used to enhance or reuse component logic across different components without modifying the original component. For example, an HOC can add loading functionality to a component by wrapping it and conditionally rendering a loading indicator.
\
What is code splitting in React?Code splitting in React is a technique to improve performance by breaking up the application into smaller chunks that are loaded on demand. This is achieved using React.lazy and Suspense, which dynamically import components and show a fallback UI while they are being loaded.
\
What is Redux?Redux is a state management library for JavaScript applications, often used with React. It provides a centralized store for all application state, making it easier to manage and debug complex states.
\
How Redux Works?Redux works by following a unidirectional data flow:
\
What is Redux Toolkit?Redux Toolkit is a library that simplifies using Redux by providing tools like createSlice for reducing boilerplate code, configureStore for setting up the store, and built-in support for handling asynchronous logic. It streamlines state management in Redux and improves the development experience.
\
What is the use of the useCallback hook in React?The useCallback hook is used to memoize a function, preventing it from being recreated on every render unless its dependencies change. This is useful for optimizing performance, especially when passing functions to child components.
\
What is the use of the useMemo hook in React?The useMemo hook in React is used to memoize expensive calculations and prevent unnecessary re-computation. It recalculates the result only when its dependencies change, improving performance by avoiding re-renders of components with heavy computations.
\
What is React.memo and how is it used for performance optimization?React.memo is a higher-order component (HOC) in React that optimizes performance by preventing unnecessary re-renders of a component. It works by memoizing the rendered output of a functional component and only re-renders it when its props change.
\
Explain provider and consumer in Context API.In React's Context API, the Provider supplies data to components, while the Consumer accesses that data. The Provider wraps components and passes a value prop, and the Consumer retrieves that value. In functional components, the useContext hook is often used instead of Consumer for easier access to context.
\
Explain the Concept of Reconciliation in React.Reconciliation is the process React uses to update the DOM by comparing the new and previous virtual DOM trees and applying the minimal set of changes needed to the real DOM.
\
What is the role of TypeScript in improving React code safety and scalability?TypeScript improves React code safety and scalability by adding static type checking, which helps catch errors during development rather than at runtime. It ensures that components receive the correct props, enforces consistent data structures, and improves code maintainability. TypeScript also enhances scalability by making refactoring easier and improving code readability through type definitions.
\
Why is It Important to Test Your React Application?Testing ensures that your React application works as expected. It helps catch bugs early, ensures that individual components function correctly, and maintains application stability as the codebase grows. Testing also provides confidence when refactoring or adding new features, as it can quickly identify unintended side effects.
\
What Would You Do If Your React Application is Rendering Slowly?If a React application is rendering slowly, you can:
\
Describe how to implement lazy loading in React using the lazy function and the Suspense component.To implement lazy loading in React, use the React.lazy function to dynamically import components and the Suspense component to handle the loading state while the component is being loaded.
\
\
\ This ensures that LazyComponent is only loaded when needed, optimizing performance.
\
How do you write a simple test in Jest1. Create a function to test:
// sum.js export function sum(a, b) { return a + b; }\ 2. Write the test:
// sum.test.js import { sum } from './sum'; test('adds 1 + 2 to equal 3', () => { expect(sum(1, 2)).toBe(3); });\ 3. Run the test:
In your terminal, run:
npm run test\
Build a Component that Uses an Effect to Perform Cleanup import React, { useState, useEffect } from 'react'; const Timer = () => { const [count, setCount] = useState(0); useEffect(() => { // Set up an interval const intervalId = setInterval(() => { setCount((prevCount) => prevCount + 1); }, 1000); // Cleanup function to clear the interval on component unmount return () => { clearInterval(intervalId); }; }, []); // Empty dependency array means the effect runs once on mount return (\
Create a dobounced function and throttle function in two inputs one debounce and one throttle, and print the values. import React, { useState, useCallback } from 'react'; // Debounce function const debounce = (func, delay) => { let timeout; return (...args) => { if (timeout) clearTimeout(timeout); timeout = setTimeout(() => { func(...args); }, delay); }; }; // Throttle function const throttle = (func, delay) => { let lastCall = 0; return (...args) => { const now = new Date().getTime(); if (now - lastCall < delay) return; lastCall = now; func(...args); }; }; const DebounceThrottleInputs = () => { const [debouncedValue, setDebouncedValue] = useState(''); const [throttledValue, setThrottledValue] = useState(''); // Handle debounced input const handleDebouncedInput = useCallback( debounce((value) => { setDebouncedValue(value); }, 1000), [] ); // Handle throttled input const handleThrottledInput = useCallback( throttle((value) => { setThrottledValue(value); }, 1000), [] ); return (Debounced Value: {debouncedValue}
Throttled Value: {throttledValue}
\ How it Works:
One input is debounced, meaning it waits for 1 second of inactivity before updating the value.
The other input is throttled, meaning it only updates the value once every second, no matter how often the input changes.
\
\
Advanced How would you optimize the performance of a React application?To optimize the performance of a React application, you can follow these strategies:
\
What is reconciliation in React, and how does it contribute to performance efficiency?Reconciliation in React is the process by which React updates the DOM efficiently. It compares the new virtual DOM (after state or props changes) with the previous virtual DOM to determine the minimal set of changes needed to update the real DOM.
\
What is list virtualization in React and how does it help improve performance when rendering large numbers of elements?List virtualization in React is a performance optimization technique used to efficiently render large lists or grids of elements by only rendering visible items in the viewport. Instead of rendering the entire list at once, virtualization ensures that only a small subset of items are rendered at a time, reducing the number of DOM nodes and improving performance.
\
What is the difference between React’s memoization, useMemo, and useCallback, and when is it appropriate to use each?Use React.memo for components, useMemo for expensive calculations, and useCallback for stable functions passed to child components.
\
Explain Redux architecture and the relationship between reducers, actions, and stores.Action: An object that describes what happened in the application, containing a type and optional payload.
Store: A centralized place to hold the application state, managed by reducers.
Reducer: A pure function that takes the current state and an action, then returns a new state based on the action type.
\
Server-Side Rendering (SSR) in React refers to the process of rendering React components on the server and sending fully rendered HTML to the browser. This contrasts with traditional client-side rendering, where React components are rendered on the client after the JavaScript bundle is downloaded.
How SSR Works:
On the server, React components are rendered into HTML.
This HTML is sent to the client.
The browser displays the HTML content immediately (providing faster initial load).
React then "hydrates" the app by attaching event handlers and making it interactive.
\
Use Cases:
Improved SEO: SSR is beneficial for SEO because search engines can index the fully rendered content, as opposed to waiting for JavaScript to load.
Faster Initial Page Load: Since the browser gets fully rendered HTML, users can see the content faster, especially on slower networks or devices.
Better Performance for Large Apps: SSR can help in rendering large applications quickly by offloading the initial rendering to the server.
\
Here are some common anti-patterns in React development to avoid:
\ 1. Mutating State Directly
\ 2. Overusing State
\ 3. Unnecessary Re-renders
\ 4. Ignoring Key Prop in Lists
\ 5. Complex Component Logic
\ 6. Using Inline Functions in Render
\ 7. Not Using Error Boundaries
8. Over-fetching Data
\
What are Web Workers and how can they be used in React to perform background operations and avoid blocking the user interface during computationally intensive tasks?Web Workers are a feature of web browsers that allow JavaScript to run in the background, on a separate thread from the main execution thread. This enables you to perform computationally intensive tasks without blocking the user interface, leading to a smoother user experience.
\ Benefits of Using Web Workers
\
Explain the difference between useLayoutEffect and useEffect. When would you prefer one over the other?useEffect:
\ useLayoutEffect:
\ Example:
import React, { useEffect, useLayoutEffect, useRef, useState } from 'react'; function Example() { const [height, setHeight] = useState(0); const divRef = useRef(null); // useLayoutEffect: Measure DOM before painting useLayoutEffect(() => { const measuredHeight = divRef.current.getBoundingClientRect().height; setHeight(measuredHeight); }, []); // useEffect: Fetch data after render useEffect(() => { // Fetch some data here console.log("Data fetched after render"); }, []); return (\ In this example:
\
How would you handle preloading components or data to improve the user experience in a React application?To improve user experience in a React application by preloading components or data, you can apply several strategies such as lazy loading, code splitting, and prefetching data or components. These techniques help ensure that resources or data are available ahead of time, reducing wait times and enhancing the overall performance.
\ 1. Preloading Components:
Preloading or pre-rendering components that the user is likely to interact with can prevent delays when those components are needed.
Using React.lazy() and Suspense: Normally, React.lazy loads components on demand. You can combine lazy loading with preloading to ensure components are loaded ahead of time (e.g., when hovering over a button).
\
\ 2. Preloading Routes:
In large apps, you may use code splitting to load routes only when needed, but preloading them for smoother transitions can help avoid delays.
React Router with Route Preloading: Combine React.lazy() with dynamic imports to preload route components that are likely to be visited soon.
\
\ 3. Preloading Data:
You can preload data in advance by fetching data before the user navigates to the relevant section, avoiding waiting times for data-heavy operations.
Using React Query for Prefetching: Libraries like React Query allow you to prefetch data before it’s needed, improving UX by ensuring data is already available when the component is rendered.
\
\ 4. Prefetching Assets (Images, Fonts, etc.):
Preloading images, fonts, or other assets ensures smoother transitions when components are displayed. You can use in the HTML or JavaScript APIs to preload assets.
\
function preloadImage(src) { const img = new Image(); img.src = src; } function ImageComponent() { const imageUrl = 'https://example.com/image.jpg'; preloadImage(imageUrl); // Preload the image return ; }\ 5. Prefetching with rel="prefetch":
Modern browsers support prefetching of resources (JS, CSS, etc.). You can add rel="prefetch" links to preload components that the user might need later.
\
\ Summary of Preloading Strategies:
\
How would you optimize the rendering of a component that frequently receives new props or state updates?To optimize the rendering of a component that frequently receives new props or state updates:
\
What are some common performance bottlenecks in large React applications, and how would you address them?Common performance bottlenecks in large React applications and ways to address them include:
\
How do you manage deeply nested or complex global state in React using Context API without causing unnecessary re-renders?Managing deeply nested or complex global state in React using the Context API while minimizing unnecessary re-renders can be achieved through several strategies:
Example:
import React, { createContext, useContext, useState, useMemo } from 'react'; // Create context const GlobalContext = createContext(); export const GlobalProvider = ({ children }) => { const [state, setState] = useState({ user: null, theme: 'light', // other state... }); const value = useMemo(() => ({ user: state.user, theme: state.theme, setUser: (user) => setState((prev) => ({ ...prev, user })), setTheme: (theme) => setState((prev) => ({ ...prev, theme })), }), [state.user, state.theme]); // Only update when these change return (\
How would you handle error boundaries in React? Can you explain a real-world scenario where error boundaries are crucial?In React, error boundaries are used to catch JavaScript errors in components and provide a fallback UI instead of crashing the entire application. You can implement error boundaries by creating a class component that implements the componentDidCatch lifecycle method and the getDerivedStateFromError static method.
\ Implementation of an Error Boundary:
import React from 'react'; class ErrorBoundary extends React.Component { constructor(props) { super(props); this.state = { hasError: false }; } static getDerivedStateFromError(error) { // Update state so the next render shows the fallback UI return { hasError: true }; } componentDidCatch(error, errorInfo) { // Log the error to an error reporting service console.error("Error logged:", error, errorInfo); } render() { if (this.state.hasError) { // Fallback UI return\ Usage:
Wrap components that might throw errors with the ErrorBoundary:
function App() { return (\ Example: E-Commerce Checkout Process: In a large e-commerce application, the checkout process involves multiple components: cart summary, payment processing, and order confirmation. If a user encounters an error during payment processing (e.g., a network issue or a validation error), an error boundary can catch this error without crashing the entire checkout flow.
Without error boundaries, a JavaScript error could prevent the user from completing their purchase, leading to a poor user experience and potential loss of sales. By using error boundaries, you can display a friendly error message, allow the user to retry the payment, or redirect them back to the cart, ensuring the application remains stable and functional.
\
How would you debug performance issues in a React application, and what tools would you use?Debugging performance issues in a React application involves several steps and tools to identify and resolve bottlenecks.
\ 1. Identify Performance Issues:
\ 2. Use React DevTools:
\ 3. Chrome DevTools:
\ 4. Lighthouse:
\ 5. Analyze Network Requests:
\ 6. Code Analysis:
\ 7. Monitor with External Tools:
\ 8. Profiling and Optimization:
\
How would you test components that use hooks like useEffect or useState with Jest and React Testing Library?To test components that use hooks like useEffect or useState with Jest and React Testing Library, you need to focus on testing how the component behaves when the state changes or side effects are triggered, rather than testing the implementation details of the hooks themselves. The idea is to ensure that your component works as expected with the hooks.
\ 1. Testing useState:
For components that use useState, the goal is to test how the component's state changes based on user interactions or other events.
import React, { useState } from 'react'; function Counter() { const [count, setCount] = useState(0); return (Count: {count}
\ Test for useState with Jest and React Testing Library:
import { render, screen, fireEvent } from '@testing-library/react'; import Counter from './Counter'; test('increments count when button is clicked', () => { render(\ In this example:
We render the Counter component.
Use fireEvent.click() to simulate a user interaction that triggers a state change.
Check that the UI updates as expected after the state change.
\
2. Testing useEffect:
For components using useEffect, you can test how the component behaves after the effect is triggered (e.g., fetching data, subscribing to events).
\
import React, { useEffect, useState } from 'react'; function DataFetcher() { const [data, setData] = useState(null); useEffect(() => { async function fetchData() { const response = await fetch('/api/data'); const result = await response.json(); setData(result); } fetchData(); }, []); if (!data) { return\ Test for useEffect with Jest and React Testing Library:
To test asynchronous operations like fetching data, you’ll use mocking with Jest. You can mock the fetch API and control its return values to simulate different scenarios (like success or failure).
\
import { render, screen, waitFor } from '@testing-library/react'; import DataFetcher from './DataFetcher'; // Mock the fetch API global.fetch = jest.fn(() => Promise.resolve({ json: () => Promise.resolve({ name: 'Test Data' }), }) ); test('renders fetched data after loading', async () => { render(\ In this example:
\ 3. Testing Custom Hooks:
If you have a custom hook, it’s a good practice to extract the logic from the component and write unit tests specifically for the hook.
You can use @testing-library/react-hooks for testing custom hooks.
\
import { useState, useEffect } from 'react'; function useFetchData(url) { const [data, setData] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { async function fetchData() { const response = await fetch(url); const result = await response.json(); setData(result); setLoading(false); } fetchData(); }, [url]); return { data, loading }; } export default useFetchData;\ Test for the custom hook:
import { renderHook } from '@testing-library/react-hooks'; import useFetchData from './useFetchData'; global.fetch = jest.fn(() => Promise.resolve({ json: () => Promise.resolve({ name: 'Test Data' }), }) ); test('should fetch and return data', async () => { const { result, waitForNextUpdate } = renderHook(() => useFetchData('/api/data')); // Initially, loading should be true expect(result.current.loading).toBe(true); // Wait for the hook to finish the data fetching await waitForNextUpdate(); // After the update, data should be present and loading should be false expect(result.current.loading).toBe(false); expect(result.current.data).toEqual({ name: 'Test Data' }); });\ In this example:
\ Summary of Steps to Test Components with Hooks:
Description: Create a custom hook called useForm to manage complex forms with validation and state management. The hook should:
Support real-time field validation.
Handle state for multiple fields (e.g., Name, Email, Password).
Display error messages when validation fails.
Provide a way to reset the form to its initial state.
\
Requirements:
The form should include fields: Name, Email, and Password.
The hook should accept a validation object that contains the validation rules for each field.
Display error messages under each field if it does not meet the validation rules.
Include a button to reset the form.
\
Solution:
import { useState } from 'react'; // Custom hook for form management function useForm(initialState, validate) { const [values, setValues] = useState(initialState); const [errors, setErrors] = useState({}); const handleChange = (e) => { const { name, value } = e.target; setValues({ ...values, [name]: value, }); if (validate[name]) { const error = validate[name](value); setErrors({ ...errors, [name]: error, }); } }; const handleReset = () => { setValues(initialState); setErrors({}); }; return { values, errors, handleChange, handleReset, }; } // Validation rules const validate = { name: (value) => (value ? '' : 'Name is required'), email: (value) => /\S+@\S+\.\S+/.test(value) ? '' : 'Invalid email address', password: (value) => value.length >= 6 ? '' : 'Password must be at least 6 characters', }; // Form Component using useForm function Form() { const initialState = { name: '', email: '', password: '' }; const { values, errors, handleChange, handleReset } = useForm( initialState, validate ); return ( ); } export default Form;\
Optimizing a List of Elements with VirtualizationDescription: You have a list of 10,000 items that need to be rendered on a page. Implement a component that optimizes the rendering of this list using "list virtualization" techniques. The component should load only the visible items in the viewport and render additional elements as the user scrolls.
\ Requirements:
\ Solution using react-window:
First, install the library:
npm install react-window\ Then, implement the component:
import React from 'react'; import { FixedSizeList as List } from 'react-window'; // Sample data: Array of 10,000 items const items = Array.from({ length: 10000 }, (_, index) => `Item ${index + 1}`); function Row({ index, style }) { return (\
We used the react-window library to handle list virtualization for a large list of 10,000 items.
Only the items visible in the viewport are rendered, improving performance significantly by reducing the number of DOM elements.
\
\
All Rights Reserved. Copyright , Central Coast Communications, Inc.