Font size
WorksheetsReact Interview
Total questions: 93
Worksheet time: 47mins
What is React?
A library for handling HTTP requests
A library for building user interfaces
A database management system
A CSS preprocessor
What are components in React?
Reusable UI elements
The logic part of React
Database structures
React's core library code
What is JSX?
JavaScript extension
HTML preprocessor
XML-like syntax to write React components
JavaScript compiler
What is the difference between functional and class components?
Functional components are stateful
Functional components are stateless
Class components are only for styling
Class components can't manage state
How do you pass data to components in React?
Through props
Through events
Through context
Directly by DOM manipulation
What is the correct syntax for using a prop in a functional component?
this.props.name
props.name
{props}.name
props().name
What is the use of `useState` in React?
To manage component lifecycle
To manage component state in functional components
To bind event handlers
To handle API requests
Which hook is used for side effects in React?
useState
useEffect
useContext
useReducer
What does `useEffect` do?
Handles event listeners
Fetches data and runs side effects
Manages state
Handles component updates
How are state updates managed in functional components?
this.setState
useState()
setEffect()
setReducer()
What is the purpose of `useContext` in React?
To manage state globally
To share data across components without props
To render components conditionally
To handle form inputs
What is React’s Virtual DOM?
The real DOM used in browsers
A copy of the actual DOM for performance optimization
A framework for data storage
A database engine
How does React differ from other UI libraries?
It focuses on state management only
It uses a Virtual DOM for faster updates
It directly manipulates the DOM
It’s only used for mobile apps
What does React.memo() do?
Memoizes expensive calculations
Optimizes functional components by memoizing them
Stores variables in memory
Handles asynchronous tasks
How do you conditionally render a component in React?
Using if-else
Using try-catch
Using ternary operators or short-circuit evaluation
Using switch-case
What is the role of `key` in lists in React?
To track elements in the DOM efficiently
To manage state
To manage CSS classes
To set element attributes
What happens when you change the state in React?
The component rerenders
The state is reset
The DOM updates instantly
The component is destroyed and recreated
How can you optimize the performance of a large React application?
By adding more components
By using React.memo
By removing all keys from lists
By using class components exclusively
What does `useReducer` do?
Handles API requests
Provides an alternative to `useState` for complex state logic
Optimizes component rendering
Manages form validation
How does React handle forms?
With form context
With controlled or uncontrolled components
By using `useForm` hook
Through context API
What are controlled components?
Components where form data is handled by the DOM
Components where form data is handled by React state
Components that don’t accept props
Components that don’t have state
What is a PureComponent?
A component that ignores prop changes
A component that renders only once
A component that implements `shouldComponentUpdate` for performance
A component without any children
What is the Context API?
A method to optimize re-rendering
An API to manage component styles
An API to pass data deeply without prop drilling
A way to fetch data from an API
What is the difference between `useEffect` and `useLayoutEffect`?
`useEffect` runs before DOM painting
`useLayoutEffect` runs after painting
`useEffect` runs after DOM painting
`useLayoutEffect` runs before rendering
How can you fetch data in React?
Using `fetch` or `axios` inside `useEffect`
Directly in render method
Using `useState`
Using `useReducer` in class components
What is React.StrictMode?
A mode that speeds up the app
A mode that helps identify potential problems
An API for strict typing
An alternative to development mode
What does `React.Fragment` do?
Adds unnecessary divs to the DOM
Groups elements without adding extra nodes to the DOM
Prevents re-renders
Handles API requests
How can you share logic between components?
Using `useState`
Using `useContext`
Using custom hooks
Using `useEffect`
What is reconciliation in React?
The process of updating the actual DOM
The process of combining multiple components
The process of comparing Virtual DOM with actual DOM
The process of making components faster
What is ReactDOM used for?
Managing the Virtual DOM
Rendering components to the real DOM
Handling CSS in React
Managing Redux store
How can you force a re-render in React?
`forceUpdate()`
`setState()` with the same state
By returning null
By directly manipulating the DOM
What is lazy loading in React?
Loading components or data only when needed
Loading data at the beginning of the application
Optimizing performance by caching components
Loading components asynchronously in development
What are React portals?
A way to optimize rendering
A way to render components outside the root DOM node
A way to pass props to children
A way to manage child components
What is the role of `shouldComponentUpdate`?
Determining if a component should re-render
Forcing a component to update
Blocking component updates
Optimizing child components
How can you handle errors in React?
Using try-catch blocks in JSX
Using error boundaries
Using `useState` for errors
By throwing errors directly
What is the use of `forwardRef`?
To manage refs in state
To pass refs to children components
To manage context API
To render components conditionally
How do you manage global state in a large React app?
Using `useState` across all components
Using `useContext` or Redux
By passing props deeply
Using `useReducer` in every component
What is the role of Redux in a React application?
Managing side effects
Managing the component lifecycle
Managing global state in a predictable manner
Handling HTTP requests
How does React Router work?
It refreshes the entire page
It manages dynamic routing without reloading the page
It forces page reloads
It handles WebSocket connections
What is the purpose of `useRef` in React?
To hold mutable values that persist between renders
To trigger re-renders
To manage state
To force component updates
How do you create a context in React?
Using `useState`
Using `createContext()`
Using `useContext()`
Using `useReducer()`
What is the function of `defaultProps`?
To set default values for props
To set default state values
To trigger re-renders
To handle form defaults
How do you lazy load a component in React?
Using `React.memo`
Using `useEffect`
Using `React.lazy` with `Suspense`
Using `useReducer`
What is the significance of prop types in React?
They ensure props are passed correctly at runtime
They allow components to accept any prop
They control component lifecycle
They optimize component rendering
What does `useImperativeHandle` do?
Allows refs to modify child component instances
Forces a component to re-render
Optimizes parent-child communication
Handles context updates
What is the difference between `state` and `props` in React?
State is immutable
Props are mutable
State is managed within the component
Props are managed within the component
How does `useCallback` work?
It memoizes functions to avoid unnecessary re-creations
It memoizes components
It handles side effects
It optimizes DOM manipulation
What is server-side rendering (SSR) in React?
Rendering the UI only on the client side
Rendering the UI on the server for better performance
Rendering only static pages
Rendering components in parallel on the client side
What is the primary use of `useMemo` in React?
To manage component state
To handle side effects
To memoize expensive calculations
To manage context
When should you use `useCallback`?
To memoize functions between renders
To optimize component rendering
To fetch data from an API
To manage global state
How does React's reconciliation algorithm work?
It compares the new tree with the previous one
It directly manipulates the real DOM
It re-renders the entire tree
It removes all components and adds them again
What is the benefit of using `React.lazy()` and `Suspense`?
To handle side effects
To lazy load components for better performance
To optimize state management
To render components based on props
What problem does the `ErrorBoundary` component solve?
It handles CSS in components
It helps in catching JavaScript errors in the component tree
It handles event propagation
It manages state globally
What does `useEffect(() => {...}, [])` signify?
Runs the effect only once after initial render
Runs on every re-render
Runs only when state changes
Runs only on component unmount
What is the difference between `useMemo` and `useCallback`?
`useMemo` memoizes values, `useCallback` memoizes functions
`useMemo` and `useCallback` both memoize values
`useMemo` handles API calls, `useCallback` does not
`useMemo` is for context, `useCallback` is for state
How does `Context.Provider` work in React?
It provides a state management solution
It provides values to components that subscribe to the context
It handles API calls
It deals with CSS class management
Why would you choose `useReducer` over `useState`?
When you need to handle side effects
When your state logic is complex or state depends on previous values
When your application is small
When you need to memoize functions
What is the purpose of `shouldComponentUpdate` in class components?
To determine if a component should re-render
To prevent state updates
To trigger a re-render
To update the parent component
What does the `useRef` hook do in React?
It triggers re-renders
It creates a mutable object to hold a DOM reference or persist values between renders
It manages state
It handles form data
What is the advantage of using `React.PureComponent`?
It prevents components from updating unnecessarily
It forces a re-render of the component
It handles side effects
It optimizes form submissions
When does `useEffect` with no dependencies (`[]`) run?
Only after the initial render
On every render
When the component unmounts
Whenever a state or prop changes
How do you optimize a large React app's performance?
By using `useMemo` and `useCallback`
By adding more components
By switching to class components
By preventing props from being passed down
What are higher-order components (HOCs) in React?
A component that manages forms
A component that passes props to children
A function that takes a component and returns a new component
A lifecycle method in class components
What is `useContext` used for in React?
To handle side effects
To manage state locally
To consume a context value without prop drilling
To memoize components
How does the `React.Children` utility help?
It handles state in children components
It allows us to iterate over and manipulate children components
It prevents component re-renders
It manages child lifecycle events
What is the significance of the `defaultValue` prop in React context?
It provides a fallback value if no Provider is found
It sets the initial state of a component
It triggers a re-render
It handles global state
How does `StrictMode` help in React development?
By improving app performance
By highlighting potential problems and deprecated practices in the code
By handling state optimally
By optimizing the use of context
What does the term "lifting state up" mean in React?
Moving state to a higher-level component to make it shared among child components
Moving state to Redux
Passing state via props
Storing state outside React's tree
How does React handle events differently than plain HTML?
React uses synthetic events that wrap the browser’s native event
React doesn’t support events
React uses native events directly
React only supports custom events
What is the difference between controlled and uncontrolled components in forms?
Controlled components have their form data managed by the DOM
Uncontrolled components use state
Controlled components manage form data through React state
Uncontrolled components manage state on their own
How does `useImperativeHandle` help with refs in React?
It prevents refs from being passed to children
It allows you to customize the ref object passed to a child component
It allows event handling on refs
It manages state in refs
How do you handle API requests in React?
Using `useState`
Using `useEffect` for side effects
Using `useContext`
Using React.memo for API calls
What is server-side rendering (SSR) in React?
Rendering components on the server and sending a fully-rendered page to the client
Rendering components in parallel
Rendering components only on the client
Rendering components asynchronously
What is a higher-order function in JavaScript?
A function that takes another function as an argument
An event handler function
A lifecycle method
A CSS-related function
Why is immutability important in React state?
It allows for shallow comparison, making re-renders more efficient
It prevents memory leaks
It makes state updates faster
It prevents UI rendering issues
How can you prevent a component from re-rendering in React?
Using `React.memo()`
Using `useContext`
Using `useState` to stop updates
Using `forceUpdate()`
What does `React.lazy()` help with?
Optimizing forms
Lazy loading components for better performance
Handling global state
Managing refs
What are React portals?
A way to pass props down to deeply nested components
A way to render children outside the DOM hierarchy of the parent component
A way to handle form submissions
A way to manage component updates
How do you handle error boundaries in React?
By wrapping components with `ErrorBoundary`
By using `useEffect`
By using `useCallback`
By handling state in `useState`
What is the purpose of `PropTypes` in React?
To validate props passed to a component
To set initial state values
To manage events
To force re-renders
What is the React Fiber architecture?
React's way of handling forms
A new algorithm for better managing component rendering
React’s state management library
React’s lifecycle method
Why would you use `React.StrictMode`?
To prevent event handling errors
To highlight potential issues and avoid side effects in code
To optimize class component rendering
To manage API calls
How do you handle multiple state variables in a functional component?
Using multiple `useState` calls
Using a single `useState` with an object
Using `useReducer`
Using `useEffect` for each state
What does `forwardRef` enable in React?
It allows a parent component to pass a ref to its child
It passes state down to children
It manages global context
It memoizes child components
How do you achieve conditional rendering in React?
Using `useEffect`
Using `if` statements
Using ternary expressions or logical `&&` operators
Using context providers
What are fragments used for in React?
To group multiple elements without adding extra nodes to the DOM
To memoize state
To manage global state
To pass props to children
Why should you avoid using index as a key in lists?
It causes performance issues
It creates DOM nodes dynamically
It can cause issues with component identity during re-rendering
It prevents re-renders
How does `useEffect` cleanup function work?
It resets the component state
It runs after the component unmounts or before re-running the effect
It manages global state
It prevents re-rendering of the component
How can you optimize component re-renders in React?
Using `useCallback` and `useMemo`
By passing props deeply
Using class components only
By manually manipulating the DOM
What is React’s `Profiler` used for?
To identify performance bottlenecks in React applications
To handle global state
To optimize forms
To manage refs and state
What is the difference between `getDerivedStateFromProps` and `componentDidMount`?
`getDerivedStateFromProps` updates state before rendering based on props, `componentDidMount` runs after the component mounts
`getDerivedStateFromProps` runs after rendering, `componentDidMount` is only for updates
`getDerivedStateFromProps` manages state globally, `componentDidMount` manages local state
How does React handle memory leaks?
React automatically detects and handles them
By using cleanup functions inside `useEffect`
By triggering a re-render
By using `useState` hooks for memory management
What is the difference between `componentWillUnmount` and `useEffect` cleanup?
`componentWillUnmount` runs only in class components, `useEffect` cleanup runs in functional components
`componentWillUnmount` prevents re-rendering, `useEffect` cleanup resets state
`componentWillUnmount` handles side effects
