React 1 Flashcards

1
Q

What is React?

A

React is a JavaScript library for building user interfaces, developed by Facebook. It allows developers to create reusable UI components and efficiently update the UI in response to changes in application state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain the key features of React.

A

Key features of React include:

Virtual DOM for efficient DOM manipulation
Component-based architecture for building reusable UI elements
Declarative syntax using JSX
One-way data flow through props
Lifecycle methods for managing component behavior

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the differences between React and other JavaScript frameworks like Angular or Vue.js?

A

React uses a virtual DOM, while Angular and Vue.js use a real DOM.
React emphasizes component-based architecture, while Angular uses a MVC pattern and Vue.js uses a MVVM pattern.
React uses JSX for templating, while Angular uses HTML templates and Vue.js supports both templates and JSX.
React’s state management can be handled using libraries like Redux, whereas Angular has built-in tools for state management.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is JSX in React?

A

JSX (JavaScript XML) is a syntax extension for JavaScript that allows developers to write HTML-like code within JavaScript. It makes React component code more readable and expressive.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does React handle components?

A

React components are reusable, self-contained units of UI that can be composed together to build complex UIs. Components can be either class components or functional components.`

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a virtual DOM in React?

A

The virtual DOM is a lightweight, in-memory representation of the actual DOM. React uses the virtual DOM to perform efficient updates to the UI by calculating the difference between the virtual DOM and the actual DOM and only applying the necessary changes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain the lifecycle methods of a React component.

A

React component lifecycle methods allow developers to hook into various stages of a component’s life, such as initialization, updating, and unmounting. Some commonly used lifecycle methods include componentDidMount, componentDidUpdate, and componentWillUnmount.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are hooks in React? How do they differ from class components?

A

Hooks are functions that allow functional components to use state and other React features without writing a class. They enable developers to reuse stateful logic across components, making code more modular and readable compared to class components.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are controlled and uncontrolled components in React?

A

Controlled components are React components where form data is controlled by React state. Uncontrolled components are components where form data is handled by the DOM itself.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Explain the concept of state in React.

A

State in React is an object that represents the current state of a component. It can be modified using the setState method, and changes to state trigger re-renders of the component

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the significance of keys in React lists?

A

Keys are used in React lists to uniquely identify elements and optimize rendering performance by helping React identify which items have changed, been added, or been removed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you pass data between components in React?

A

Data can be passed between components in React using props for parent-to-child communication and callback functions or context for more complex scenarios or child-to-parent communication.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the purpose of refs in React?

A

Refs in React provide a way to access and interact with DOM elements or React components directly. They are commonly used to focus input fields, trigger imperative animations, or integrate with third-party libraries.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you handle events in React?

A

Events in React are handled similarly to regular DOM events, using camelCase event names and passing event handlers as props to components. Event handlers are defined as methods on the component class or as arrow functions in functional components.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Explain the difference between props and state.

A

Props are read-only data passed from parent to child components, while state is mutable data managed internally by a component. Props are used for passing data down the component tree, while state is used for managing component-specific data and triggering re-renders.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is Redux and how does it relate to React?

A

Redux is a predictable state container for JavaScript applications, commonly used with React for managing application state. It provides a centralized store and a set of rules for predictable state updates.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are the advantages of using Redux with React?

A

Advantages of using Redux with React include centralized state management, improved predictability and traceability of state changes, easy integration with middleware and dev tools, and support for server-side rendering.

18
Q

Explain the Flux architecture.

A

Flux is an architectural pattern for building client-side web applications, introduced by Facebook alongside React. It emphasizes unidirectional data flow, with actions triggering updates to the application state via a central dispatcher.

19
Q

What is context in React? How is it used?

A

Context in React provides a way to pass data through the component tree without having to pass props down manually at every level. It is commonly used for sharing global data, such as theme preferences or authentication status, across components.

20
Q

What is the significance of the setState() method in React?

A

The setState() method is used in React to update a component’s state and trigger a re-render. It accepts an object containing updated state values or a function that returns such an object.

21
Q

How can you optimize performance in React applications?

A

Performance in React applications can be optimized by minimizing unnecessary re-renders, using shouldComponentUpdate or React.memo for memoization, implementing lazy loading and code splitting, and optimizing network requests and data fetching.

22
Q

What are React Fragments?

A

React Fragments are a feature that allows developers to group multiple React elements without adding extra nodes to the DOM. They are useful for returning multiple elements from a component’s render method without needing to create a wrapper element.

23
Q

How do you perform server-side rendering in React?

A

Server-side rendering in React can be achieved using libraries like Next.js or Gatsby, which generate static HTML on the server and send it to the client. This improves performance and enables better SEO and initial page load times.

24
Q

What is the significance of the shouldComponentUpdate() method?

A

The shouldComponentUpdate() method is used in React to optimize rendering performance by allowing components to prevent unnecessary re-renders. It returns a boolean value indicating whether the component should re-render based on changes to props or state.

25
Q

Explain the concept of higher-order components (HOCs) in React.

A

Higher-order components are functions that take a component and return a new component with enhanced functionality. They are commonly used for code reuse, cross-cutting concerns like logging or authentication, and adding additional props or behavior to components.

26
Q

How do you handle forms in React?

A

Forms in React can be handled using controlled components, where form data is controlled by React state and updated via event handlers, or uncontrolled components, where form data is handled by the DOM. React also provides form validation and submission features.

27
Q

What are portals in React?

A

Portals in React provide a way to render children into a DOM node that exists outside the parent component’s DOM hierarchy. They are useful for implementing features like modals, tooltips, and dropdown

28
Q

Explain the significance of error boundaries in React:

A

Error boundaries are React components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of crashing the entire application. They help to isolate errors and prevent them from propagating to higher-level components, thus improving the overall robustness and stability of React applications.

29
Q

How do you handle routing in React applications?

A

Routing in React applications can be handled using various libraries such as React Router. React Router provides a declarative way to define routes and their corresponding components, allowing users to navigate between different views within a single-page application (SPA). It manages the browser’s URL and keeps the UI in sync with the URL, enabling a seamless user experience.

30
Q

What are React hooks? List some commonly used hooks:

A

React hooks are functions that enable functional components to use state and other React features without writing a class. Some commonly used hooks include:

useState: Manages component state.
useEffect: Performs side effects in functional components.
useContext: Accesses context within functional components.
useReducer: Alternative to useState for managing complex state logic.
useCallback and useMemo: Memoize functions and values to optimize performance.

31
Q

How do you handle authentication in a React application?

A

Authentication in React applications involves implementing mechanisms such as token-based authentication or session-based authentication. Typically, this includes managing user sessions, storing tokens securely (e.g., in local storage or cookies), and implementing login and logout functionality. Libraries like Auth0 or Firebase Auth can simplify the authentication process in React applications.

32
Q

What are the differences between React class components and functional components?

A

React class components are ES6 classes that extend from React.Component and have their own state and lifecycle methods. Functional components are simply JavaScript functions that take props as input and return React elements. With the introduction of React hooks, functional components can now also manage state and perform side effects, making them more versatile and easier to use than class components.

33
Q

How do you test React components?

A

React components can be tested using various testing libraries and frameworks such as Jest, Enzyme, or React Testing Library. Unit tests can be written to test individual components in isolation, while integration tests can be used to test the interaction between multiple components. Mocking and shallow rendering can be employed to isolate components and simulate different scenarios for testing.

34
Q

Explain the significance of PureComponent in React:

A

PureComponent is a class component in React that implements a shallow comparison of props and state to determine whether a component should re-render. It is optimized for performance, as it prevents unnecessary re-renders when the props and state of a component have not changed. However, it is important to note that PureComponent only performs a shallow comparison, so it may not be suitable for all use cases, especially when dealing with complex data structures or nested objects.

35
Q

What is the difference between props.children and children in React?

A

props.children is a special prop in React that represents the content passed between the opening and closing tags of a component. It can contain any React elements, including text, components, or even functions. On the other hand, children is a JavaScript property of the props object that contains the children of a component as an array-like object. props.children is more commonly used in React functional components, while children is often used in class components.

36
Q

How do you handle state management without Redux in React?

A

State management in React can be handled without Redux using various alternative approaches such as React’s built-in useState hook, context API, or other state management libraries like MobX or Zustand. Context API allows sharing state between components without prop drilling, making it suitable for managing global or shared state in smaller applications. Alternatively, libraries like MobX and Zustand offer more advanced state management capabilities with observable state and automatic reactivity.

37
Q

What are some common performance optimization techniques for React applications?

A

Common performance optimization techniques for React applications include:

Memoization using useMemo and useCallback to avoid unnecessary re-computation.
Code splitting to split the application into smaller chunks and load them asynchronously.
Lazy loading to defer the loading of components or resources until they are needed.
Virtualization techniques such as windowing or infinite scrolling to optimize the rendering of large lists or data sets.
Minimizing re-renders by using PureComponent or implementing shouldComponentUpdate or React.memo for functional components.

38
Q

What is the purpose of the dangerouslySetInnerHTML attribute in React?

A

dangerouslySetInnerHTML is a React attribute that allows inserting raw HTML directly into a component. It is typically used when integrating with third-party libraries or when rendering HTML content received from an external source, such as an API response. However, it should be used with caution, as it can expose the application to security vulnerabilities such as cross-site scripting (XSS) attacks if not properly sanitized.

39
Q

How does React handle security vulnerabilities like XSS attacks?

A

React provides built-in protections against cross-site scripting (XSS) attacks by escaping user input and sanitizing potentially dangerous content. It automatically escapes characters that could be interpreted as HTML entities, preventing them from being rendered as HTML. Additionally, React’s strict mode provides runtime checks and warnings for potential security vulnerabilities, helping developers identify and mitigate XSS risks.

40
Q

Explain the difference between client-side and server-side rendering in React:

A

Client-side rendering (CSR) involves rendering the entire React application on the client side using JavaScript, where the initial HTML content is minimal and JavaScript is responsible for fetching data and rendering the UI dynamically. Server-side rendering (SSR), on the other hand, involves rendering React components on the server and sending the pre-rendered HTML to the client, reducing the time to first paint and improving SEO and performance. SSR is often used for improving perceived performance and search engine visibility, while CSR provides more interactivity and dynamic updates.