React interview questions
React interview questions
Questions Set 1
1.What is React?
React is a JavaScript library used to build user interfaces, especially for single-page
applications (SPAs). It helps you create dynamic and interactive web apps by breaking
down the interface into small, reusable pieces called components.
Key Points:
1. Component-based: You build your UI by creating components, which are like building
blocks of your app.
2. Declarative: You tell React how the UI should look based on the data, and React
automatically updates the UI when the data changes.
3. Virtual DOM: React uses a fast, lightweight version of the real DOM to make updates
more efficient, so your app runs faster.
4. JSX: React allows you to write HTML-like code inside JavaScript, making it easier to
design and build the UI.
2. What is Usememo?
useMemo is a React hook that memoizes (remembers) the result of a calculation and only
recomputes it when its dependencies change. It helps optimize performance by preventing
expensive calculations from running on every render.
const memoizedValue = useMemo(() => expensiveFunction(), [dependency]);
expensiveFunction(): The function whose result you want to memoize.
[dependency]: An array of dependencies; the memoized value will only be recomputed
when one of these changes.
Example:
const filteredNumbers = useMemo(() => numbers.filter(num => num % 2 === 0),
[numbers]);
Here, the filtering only happens when numbers changes, not on every render.
When to Use:
To optimize performance for expensive calculations or operations.
To prevent unnecessary re-renders when the calculated value hasn't changed.
4.What is JSX ?
JSX (JavaScript XML) is a way to write HTML-like code inside JavaScript. It’s used in
React to define the structure of the UI. JSX makes it easier to create and manage
components by combining HTML and JavaScript in one place.
5. What is DOM?
DOM (Document Object Model) is a programming interface for web documents. It
represents the structure of a webpage in a tree-like format, where each element (such
as paragraphs, images, or links) is a node in the tree. The DOM allows programming
languages (like JavaScript) to interact with, modify, and update the content, structure,
and styles of a webpage.
o Full: <React.Fragment>...</React.Fragment>
Normally, refs are used to reference DOM elements, but by default, React does not
forward refs to functional components. forwardRef is a higher-order function that lets
you pass a ref through a component to one of its children.
Key Points: