React Js Interview Question
React Js Interview Question
What is the Context API in React, and when would you use it?
• Context API is a way to pass data through the component tree without manually
passing props at every level.
• It’s useful for global state or theme management where many components need
access to the same data (e.g., user authentication status, theme settings).
How do React hooks improve the way we write components?
• Hooks allow you to use state and lifecycle features in functional components,
which were previously only available in class components.
• They simplify code and make it easier to reuse stateful logic between
components without the need for complex class hierarchies or higher-order
components (HOCs).
What are HTTP methods, and how are they used in RESTful services?
• GET: Retrieve data from the server (e.g., fetching user data).
• POST: Send data to the server to create new resources (e.g., creating a new user).
• PUT: Update an existing resource on the server (e.g., updating user info).
• DELETE: Remove a resource from the server (e.g., deleting a user).
• PATCH: Partially update a resource.
What are React Portals, and when would you use them?
React Portals allow you to render components outside the DOM hierarchy of the parent
component.
They are useful for modals, tooltips, or any component that needs to overlay other
elements.
Example:
What are the best practices for caching API responses in a React application?
Use browser cache or caching libraries (e.g., SWR, React Query) to store API responses
temporarily.
Implement ETags or Last-Modified headers to validate if the cached response is still
fresh.
Cache GET requests only, and refresh cache when data changes.
What are code-splitting and lazy loading, and how do they improve performance?
Code-splitting: Breaking the code into smaller bundles that are loaded on demand.
Lazy loading: Load components only when they are needed, reducing the initial load
time.
What is the purpose of the useEffect hook, and how does it differ from lifecycle
methods in class components?
useEffect is used to perform side effects like data fetching, DOM manipulation, or
subscriptions. It combines the functionality of lifecycle methods like
componentDidMount, componentDidUpdate, and componentWillUnmount.
The main difference is that useEffect works in functional components and lets you
manage side effects more flexibly.
How do you use the useState hook to manage state in functional components?
useState is used to declare state variables in functional components.
. Explain the concept of custom hooks in React. Can you provide an example?
Custom hooks are functions that allow you to reuse logic between components. They
follow the same rules as React hooks but encapsulate complex logic. Example: A
custom hook for fetching data:
What are the advantages and disadvantages of using controlled vs. uncontrolled
components in React?
Controlled components:
Advantage: The form elements are controlled by React state, making
validation and UI updates more predictable.
Disadvantage: More boilerplate code and frequent state updates can affect
performance.
Uncontrolled components:
Advantage: Simpler and requires less code since form inputs manage their
own state.
Disadvantage: Harder to manage and validate because you don’t have direct
control over the state of form inputs.
What is the container-presentational component pattern in React?
• Presentational components: Focus on how things look (UI). They receive data and
callbacks through props.
• Container components: Focus on how things work (logic). They manage state and
pass props to presentational components.
This pattern helps separate logic from UI, making the code cleaner and more reusable.
How do you structure a large-scale React application?
Use a modular approach by dividing your app into feature-based folders (e.g.,
components, pages, services).
Use atomic design principles (atoms, molecules, organisms) for components.
Separate concerns like state management (using Redux or Context) and routing.
Ensure the project has clear folder structures for reusable components, hooks,
utilities, and styles.
13. Explain the concept of higher-order components (HOCs) in React. Can you
provide an example?
HOCs are functions that take a component and return a new component with
enhanced functionality. Example: Adding authentication to a component.
What are React Refs, and when should they be used?
Refs provide a way to access and interact with DOM elements directly or store
values between renders without causing a re-render.
Use refs for managing focus, animations, or integrating third-party libraries that
require direct DOM access.
How do you handle animations in a React application?
• Use CSS animations for simple effects.
• For complex animations, use libraries like Framer Motion or React Spring for
declarative animations.
• You can also use React Transition Group for animations when components
enter or leave the DOM.
How do you optimize the rendering performance of React components?
• Use React.memo to prevent re-rendering of functional components when props
haven’t changed.
• Optimize rendering logic by using useMemo and useCallback to memoize values
and functions.
• Avoid unnecessary state updates and avoid inline functions in render methods.
• Use lazy loading for components and code splitting to reduce the initial load
time.
22. What are some ways to manage global state in a React application?
• Context API: Useful for simple global state management.
• Redux: A popular state management library for complex state scenarios.
• Recoil: A state management library for React with a focus on performance and
scalability.
• Zustand: A simple state management library that provides a minimalistic API.
24. What are the advantages and disadvantages of using the Context API for state
management?
• Advantages:
o Simple and built into React.
o Avoids prop drilling by providing a global state.
• Disadvantages:
o Can lead to performance issues if the context value changes frequently.
o Not as powerful as Redux for complex state logic or large applications.