1. What is React?
Answer: React is a JavaScript library for building user interfaces, particularly single-page applications where data changes over time. It allows developers to create large web applications that can update and render efficiently.
2. What are the key features of React?
Answer: JSX: A syntax extension that allows mixing HTML with JavaScript.
Components: Reusable building blocks of a React application.
Virtual DOM: React's way of optimizing updates to the DOM.
One-Way Data Binding: Data flows in one direction, making the application more predictable.
State Management: Handling data within components.
3. What is the Virtual DOM?
Answer: The Virtual DOM is a lightweight copy of the actual DOM. React uses it to determine what parts of the real DOM need to be updated when the state of a component changes, leading to more efficient updates and rendering.
4. What is JSX?
Answer: JSX (JavaScript XML) is a syntax extension for JavaScript that looks similar to HTML. It's used with React to describe what the UI should look like. JSX makes the code more readable and easier to write.
5. Explain the difference between a functional component and a class component.
Answer: Functional Component: A simpler way to write components. These are just JavaScript functions that take props as an argument and return JSX. Before React 16.8, they didn’t have state or lifecycle methods, but with hooks, they can now manage state and side effects.
Class Component: A more feature-rich way to write components, which can have their own state and lifecycle methods. They are ES6 classes that extend React.Component.
6. What are hooks in React?
Answer: Hooks are functions that let you "hook into" React state and lifecycle features from function components. Common hooks include useState, useEffect, useContext, useReducer, and useRef.
7. What is the purpose of useState in React?
Answer: useState is a hook that allows you to add state to functional components. It returns an array with two elements: the current state value and a function that lets you update this value.
8. What is useEffect and how is it used?
Answer: useEffect is a hook used to perform side effects in function components. It's commonly used for data fetching, subscriptions, or manually changing the DOM. useEffect runs after every render by default, but you can control this by providing a second argument—an array of dependencies.
9. How does React handle forms?
Answer: React handles forms using controlled components, where form elements like , , and have their value controlled by the state in React. This means the state is the single source of truth for the input data.
- What is Redux and how does it relate to React? Answer: Redux is a state management library often used with React to manage the application’s global state. It follows the principles of a single source of truth, making the state predictable by using pure functions called reducers to manage state transitions.
11. What is the Context API?
Answer: The Context API is a React feature that allows you to share data across all levels of your application without having to pass props down manually at every level. It's particularly useful for theming, user authentication, and managing global data.
12. What is the difference between props and state?
Answer: Props: Short for properties, props are read-only data passed from a parent component to a child component. They cannot be modified by the child component.
State: State is a mutable data structure that holds information about the component. Unlike props, state can be changed within the component.
13. How do you optimize performance in a React application?
Answer: Use React.memo for functional components to prevent unnecessary re-renders.
Implement shouldComponentUpdate or use PureComponent in class components.
Use lazy loading and code splitting with React.lazy and Suspense.
Optimize state management and avoid deep updates in the state tree.
Use the React Developer Tools to profile and identify performance bottlenecks.
14. What is the purpose of key in React lists?
Answer: The key attribute is used by React to identify which items in a list have changed, been added, or removed. Keys should be unique among siblings and help React optimize re-rendering.
15. Explain higher-order components (HOCs) in React.
Answer: A Higher-Order Component is a pattern where a function takes a component and returns a new component with additional props or behavior. HOCs are used for reusing component logic.
Top comments (0)