React Interview Questions and Answers
React Interview Questions and Answers
Question 01
Explain what is the Virtual DOM in
React
The virtual DOM (VDOM) is a programming concept where an
ideal, or “virtual”, representation of a UI is kept in memory and
synced with the “real” DOM by a library such as ReactDOM
Question 02
What are the differences between
the Virtual DOM and the DOM?
A virtual DOM object is the same as a real DOM object, except
that it is a lightweight copy. This means that it cannot manipulate
on-screen elements. The main purpose is to perform fast
calculations in memory on the DOM elements.
Question 03
What is JSX?
with HTML like template syntax. This makes the HTML file
Question 04
Render is just a fancy word for saying that React is calling one
of your components.
<Modal <Modal
state of <Modal
changes
(state) (state)
Question 05
Explain the concept of Components
in React
Components are the building blocks of a React application’s
UI. These components split up the entire UI into small
independent and reusable pieces.
Question 06
What are props in React and what
are the main differences with state?
Props is the shorthand for Properties in React. They are read-
only variables which must be kept pure i.e. immutable. This
help in maintaining the unidirectional data flow and are
generally used to render the dynamically generated data.
Question 07
How can you update the state of a
component?
In functional components you can make use of the useState
hook to define and update a state variable. It gives back an
array that holds the state variable in the first position and the
setter or update function in the second position. You can make
use of this function to update the state variable like this:
setSomeState(’some value’).
Question 08
What are synthethic events in
React?
Synthetic events are the objects which act as a cross-browser
wrapper around the browser’s native event. They combine the
behavior of different browsers into one API. This is done to
make sure that the events show consistent properties across
different browsers.
Question 09
What are refs in React, give an
example of when would you use one.
Refs is the short hand for References in React. It is a variable
that holds a reference to a value that’s not needed for
rendering. You can define a reference using the useRef hook,
which returns a reference variable that you can access by
calling the ref.current key. A use case for using ref is when you
need to perform dom calculations on specific elements.
Question 10
How does React render process
work?
Trigger a Render
Render
React Renders Your
component/s React calls your component/s
Commit
React commits React updates the DOM
changes to the DOM
Question 11
How can you reuse logic in
functional components?
You can make use of customHooks, which are functions that
hold data that you want to reuse through multiple
components, e.g. useActiveUser() can be a custom 4 hook
that returns all the information from the custom user
Question 12
What are keys in React and why are
they important?
A key is a string or a number that uniquely identifies a React
element. Keys are important (specially in arrays) because they
tell React which array item each component corresponds to,
so that it can match them up later.
This becomes important if your array items can move (e.g. due
to sorting), get inserted, or get deleted. A well-chosen key
helps React infer what exactly has happened, and make the
correct updates to the DOM tree.
Question 15
How would you avoid unnecessary
renders in a React component?
It depends on each use case but one technique could be to
wrap the props the component receive with useCallback or
useMemo , and also use Rect.memo on the component itself,
to only re-render when xwz props change.
Question 16
What is a Portal in React?
React has a built in function called React.createPortal, that
creates a bridge between the component where it’s called and
some DOM element, the second parameter accepts the node
element we want to render.
useEffect useLayoutEffect
State Changes State Changes
Question 19
What are hooks in React?
Hooks is React’s way to manage state and side effects in
functional components. Hooks are named functions that start
with the word use and allow us to reuse stateful logic across
components without having to write a class component
Question 21
What is prop-drilling, and how you
can avoid it?
Prop drilling is the unofficial term for passing data through
several nested children components, in a bid to deliver this
data to a deeply-nested component.
There are several ways you can avoid this, one could be using
a centralized store for the state management of your up, like
Redux. Another solution could be by making use of React’s
composition model.
Question 22
How can you reset a component’s
state to it’s initial state?
React state tips!
Resetting a component’s
state with a key
Whenever you pass in a different key to a
component you are resetting the
component’s state.
@_georgemoller
Question 23
Explain the difference between
useState and useEffect.
useState is a react built in hook that lets you add a state
variable to your component.
Question 24
If you have to set a key value in a
component, from where would you
get that value?
Different sources of data provide different sources of keys:
Question 28
At what point does the useEffect
cleanup function run?
The cleanup function runs not only during unmount, but
before every re-render with changed dependencies
Question 29
Explain the concept of Reducers
and Context in React
Reducers let you consolidate a component’s state update
logic. Context lets you pass information deep down to other
components. You can combine reducers and context together
to manage state of a complex screen.
Question 32
How does useEffect check changes
in its array dependency?
useEffect Will compare each dependency with its previous
value using the Object.is comparison. Object and Arrays are
compared by reference, and primitive variables are compared
by value