React Interview Questions? - Page-0001 (20 Files Merged)
React Interview Questions? - Page-0001 (20 Files Merged)
io
eBook
Our goal is to cover the most frequent questions that pop-up on React
interviews. In addition to that, we are providing the answers that are
deep enough to understand. We are also providing the resources that
will help you dive even deeper.
Ths eBook will evolve as we grow, as technology changes and with your
feedback. This is the first version of this eBook.
As a bonus, at the end of this eBook you willfind links to resources where
you will be able to find remote React jobs.
Thank you!
The Reacting Point - Codaroo.io
eBook
Virtual DOM works by comparing the current virtual DOM tree with a new
virtual DOM tree, and then applying the minimal set of changes to the actual
DOM. This allows React to efficiently update the user interface without
causing unnecessary re-renders or layout changes.
The Reacting Point - Codaroo.io
eBook
Virtual DOM
Browser
DOM
Real DOM is the actual tree-like structure of a web page, which can be
manipulated directly to change the layout or content of the page. Virtual
DOM is a lightweight in-memory representation of the actual DOM, which is
used to optimize the performance of updates to the user interface.
9. What is reconciliation?
Reconciliation is the process that React uses to determine the minimal set
of changes to be made to the actual DOM. It compares the current virtual
DOM tree with a new virtual DOM tree, and then applies the minimal set of
changes to the actual DOM.
The Reacting Point - Codaroo.io
eBook
Update
Reconciliatio
n New Virtual
DOM Browser DOM
When a component's state or props change, React will create a new virtual
DOM tree, compare it to the previous tree, and then apply only the minimal
set of changes necessary to the actual DOM. This improves the
performance of the application and reduces the number of unnecessary re
renders.
Class components in React are defined using the ES6 class syntax. They
have access to state and lifecycle methods and are used for more complex,
stateful components .
State in React is an object that holds data that can change, and it is
managed by a component. It is used to store and update the
component's data and can be passed down to child components as
props.
State can be updated using the setState method, which triggers a re-render
of the component, updating the user interface.
Props in React are used to pass data from a parent component to a child
component. They are essentially a way to pass data and methods down the
component tree. Props are read-only, meaning that they cannot be modified
by the child component.
The children prop is a special prop that is used to pass children elements to
a component. It is used to pass elements between components, such as a
list of items or a set of nested components.
The Reacting Point - Codaroo.io
eBook
To create a props proxy for an HOC component, you can use the
React .f orwardRef function. This function allows you to pass props
through to the wrapped component, preserving the original component's
props.
To update state in a React class component, you can use the setstate
method. This method takes an object or function as an argument, and it
will merge the new state with the existing state.
State is the internal data of a component that can change and is managed
by the component itself. Props are external data passed to a component
from its parent component. State can be updated by the component,
whereas props cannot be updated by the component.
React Hooks are functions that allow you to use state and other React
features in functiona l components. They were introduced in React 16.8.
The useEffect hook allows you to run side effects, such as fetching data or
updating the DOM, in a functional component. It takes a callback function
as its first argument, which is called after the component has rendered.
Here's an example of how to use the useEffect hook:
The useEffect hook is used to run an async function fetchData that fetches
data from an API and updates the component's state with the result. The
empty array [] passed as the second argument to useEffect means that the
effect will only run once, when the component is first rendered.
The useEffect hook allows the component to update its state and re-render
in response to changes in some variable. In this case, when the component
is first rendered, the data is fetched and the component re-renders with the
updated data.
The Reacting Point - Codaroo.io
eBook
In this example, the useMemo hook is used to memoize the result of a costly
calculation that depends on the values of a and b. The useMemo hook takes
two arguments: the first is a function that performs the calculation, and the
second is an array of dependencies.
The Reacting Point - Codaroo.io
eBook
The component has two input fields, where the user can set the values of a
and b. When either of the inputs change, the component re-renders, but the
result is only recalculated if a or b have changed, which is determined by
the dependencies array [a,b].
This prevents the costly calculation from being performed every time the
component re-renders, improving the performance of the application.
The Reacting Point - Codaroo.io
eBook
To create a ref, you can use the useRef hook, which returns a ref object.
You can then assign this object to a ref attribute on a JSX element.
The Reacting Point - Codaroo.io
eBook
Yes, you can create your custom React hooks by following the naming
convention "use" and using state and other hooks inside it.
When creating custom React hooks, it's important to keep in mind that
they should only call other hooks at the top level and not inside loops or
conditions.
Also, it's important to make sure that the hook only performs one specific
action.
The Reacting Point - Codaroo.io
eBook
The Context API in React allows you to share data between components
without passing props through every level of the component tree. It
provides a way for components to access data that is "global" to the
component tree, such as a user's authentication status or a theme.
The Context API consists of a Provider component, which provides the data,
and a Consumer component, which accesses the data.
React Router is a library for routing in React apps. It allows you to define the
different routes in your application and render the appropriate components
for each route. This makes it easy to change the displayed content based
on the current URL, without having to refresh the page.
Pure components are components that only re-render if their props or state
have changed. They are optimized for performance, and they can improve
the performance of your application by reducing the number of
unnecessary re-renders.