React Flashcards
What is React and why is it popular?
React is a JavaScript library developed by Facebook for building user interfaces, especially for single-page applications. It’s popular because of its component-based architecture, efficient rendering using the virtual DOM, and flexibility to integrate with various backends.
Explain the virtual DOM and its significance in React.
The virtual DOM is an in-memory representation of the real DOM. Instead of making direct changes to the actual DOM, React updates the virtual DOM. This allows React to determine the most efficient way to make changes to the actual DOM by comparing the current virtual DOM with the next version, a process called “reconciliation.” This results in faster and more efficient updates.
What is JSX in React? How is it different from HTML?
JSX (JavaScript XML) is a syntax extension for JavaScript used in React to describe UI components. While it resembles HTML, it allows for the embedding of JavaScript expressions. JSX produces React elements, whereas HTML produces browser DOM elements. In JSX, properties and methods are camelCased rather than lowercased as in HTML.
How do you create a React component?
A React component can be created using a class or a function. The component returns a React element, typically written in JSX, which defines how the component should appear. Each component should also include a ‘render’ method when using class components.
What is the purpose of the render method in a React component?
The render method specifies the UI output of a React component. It returns a single React element (either a native DOM element or another component) that represents the component’s visual representation.
What is the significance of state in React?
State in React represents the data that can change over time within a component. It determines the component’s behavior and rendering. Whenever state changes, the component re-renders to reflect those changes.
What is a functional component in React?
A functional component is a simpler way to write a React component. It’s just a JavaScript function that returns a React element. Unlike class components, functional components don’t have a ‘render’ method and don’t manage their own state or lifecycle methods without hooks.
How do you pass data from a parent component to a child component?
Data is passed from a parent component to a child component through “props”. The parent component defines the props for the child component, and the child accesses them as arguments to its function or through ‘this.props’ in class components.
Explain the concept of props in React.
Props (short for “properties”) are a way of passing data from parent to child components in React. They are read-only and help to make components reusable by giving components the ability to receive data from its parent component.
What is React’s key principle for optimizing performance?
React’s key principle for optimizing performance is its use of the virtual DOM and the reconciliation process. By updating only what’s necessary in the real DOM, based on differences between the current virtual DOM and the next version, React ensures efficient and optimal rendering.
What are controlled components in React?
Controlled components in React are form elements like input, textarea, and select where the value of the element is controlled by the React state. This means the element’s value is set by the component’s state and can be modified by event handlers.
What is the purpose of the useState hook in React?
The useState hook allows functional components to use state. It returns an array where the first element is the current state value, and the second element is a function to update that state. It provides a way to add state management functionality to functional components without converting them to class components.
How do you handle events in React?
In React, events are handled using event handlers, which are JavaScript functions. These handlers can be passed as props to child components or used directly in components. React’s event system is synthetic, meaning it wraps around the browser’s native event system, providing a consistent API across browsers.
Describe the component lifecycle methods in React.
In React, class components have lifecycle methods that get executed at various stages of a component’s life, such as mounting, updating, and unmounting. Key methods include: componentDidMount, shouldComponentUpdate, componentDidUpdate, and componentWillUnmount.
Explain the difference between componentDidMount and useEffect.
componentDidMount is a lifecycle method in class components that runs after the component output is rendered to the DOM. useEffect is a hook in functional components that can mimic the behavior of various lifecycle methods, including componentDidMount, depending on its dependency array. If the dependency array is empty, useEffect acts like `componentDidMount