React JS Interview Questions and Answers
React JS Interview Questions and Answers
Basic Questions
1. What is React?
Answer: React is a JavaScript library for building user interfaces, particularly single-page applications. It
allows developers to create reusable UI components.
3. What is JSX?
Answer: JSX stands for JavaScript XML. It allows developers to write HTML elements within JavaScript
and place them in the DOM without using functions like createElement().
Answer: The render method in a class component returns the React elements that should be displayed
in the UI. It is a required method for class components.
Answer: setState is a method used to update the state of a component. It schedules an update to the
component's state object and tells React to re-render the component with the updated state.
16. What is the purpose of the constructor method in a React class component?
Answer: The constructor method is used to initialize the component's state and bind event handlers
before the component is mounted.
Answer: React is a JavaScript library, thus technically it isn't faster at the act of DOM manipulation than
JavaScript itself. React isn't faster than JavaScript. It is approximately as fast as JavaScript.
Answer: Synthetic events are React's cross-browser wrapper around the browser's native event system.
They provide a consistent interface for handling events in React components.
Answer: defaultProps is used to define default values for props in a component. It ensures that the
component has sensible defaults if certain props are not provided.
25. How do you pass data from a parent component to a child component?
Answer: Data is passed from a parent component to a child component via props. The parent
component defines the props and passes them to the child component in the JSX.
Advanced Questions
3. What is react-router?
Answer: React Router is a library for routing in React. It enables the navigation among routes in a React
Application. It allows changing the browser URL, and keeps the UI in sync with the URL.
5. What is routing?
Answer: Routing in React JS is a mechanism that allows you to navigate and display different views or
components in a single-page web application. It enables users to move between different parts of the
application without the need for a full page reload
Answer: useState is a hook that allows you to add state to function components. It returns an array
with the current state value and a function to update it.
Answer: useEffect is a hook that allows you to perform side effects in function components. It runs
after the component renders and can be used for tasks like fetching data or directly manipulating the
DOM.
Answer: Prop drilling refers to passing props through multiple intermediate components to reach a
deeply nested component. It can be avoided using context or state management libraries like Redux.
Answer: useCallback returns a memoized callback function, used to avoid recreating functions on
every render. useMemo returns a memoized value, used to avoid recomputing expensive calculations on
every render.
Answer: Axios is a popular JavaScript library used to make HTTP requests. It is similar to the fetch
method. Axios is used to fetch data from APIs and then render that data in their React components.