What are fragments?

  • A way to create custom HTML elements
  • A way to group multiple elements without adding an extra DOM node
  • An alternative to using HTML tags for styling
  • Special React components
Fragments are a way to group multiple elements without adding an extra DOM node. Fragments are useful when you want to return multiple elements from a component, but don't want to add an unnecessary container element to the DOM.

How events are different in React?

  • All the options
  • Events in React are synthetic events
  • React events are camelCase instead of lowercase
  • React events have a different API than native events
Events in React are different from native events in several ways. React events are synthetic events that are cross-browser compatible and have the same API across all browsers. React events are also camelCase instead of lowercase (e.g. onClick instead of onclick). Finally, React events have a different API than native events, including the ability to call preventDefault and stopPropagation on the event object.

How to prevent a function from being called multiple times?

  • Use memoization
  • Use shouldComponentUpdate lifecycle method
  • Use componentDidUpdate lifecycle method
  • Use a callback ref
Memoization is a technique used to optimize functions by caching the results of expensive function calls and returning the cached result when the same inputs occur again. By using memoization, a function can be prevented from being called multiple times with the same inputs.

How to structure Redux top level directories?

  • By feature
  • By layer
  • By file type
A common way to structure top-level directories in a Redux application is by feature. This means grouping all related actions, reducers, and components into a single directory for each feature of the application. This helps keep the code organized and makes it easier to reason about.

What are synthetic events in React?

  • Events that are cross-browser compatible and behave consistently across different platforms
  • Events that are not optimized for performance
  • Events that occur in a virtual environment
  • Events that trigger multiple times
Synthetic events in React are events that are cross-browser compatible and behave consistently across different platforms. They are also optimized for performance by pooling event objects, which allows them to be reused and prevents excessive memory allocation. Synthetic events have the same interface as native events, but they are implemented differently behind the scenes.

Why should not call setState in componentWillUnmount?

  • Because it will cause a memory leak
  • Because it will trigger a re-render after the component is unmounted
  • Because the component's state is no longer available after unmounting
  • Because it is unnecessary and can cause performance issues
The componentWillUnmount() method is called immediately before a component is unmounted and destroyed. It is not safe to call setState() within this method, as the component's state is no longer available after unmounting. Attempting to update the state after unmounting can cause errors or unexpected behavior in the application.

What is the difference between HTML and React event handling?

  • HTML uses camelCase event names, while React uses kebab-case
  • HTML uses inline event handlers, while React uses event listeners
  • React uses synthetic events, while HTML does not
  • There is no difference
The main difference between HTML and React event handling is that React uses synthetic events, while HTML does not. Synthetic events are cross-browser compatible and behave consistently across different platforms. They are also optimized for performance by pooling event objects.

How to use class field declarations syntax in React classes?

  • Use the constructor method
  • Use the componentWillMount lifecycle method
  • Use the componentDidMount lifecycle method
  • Use the class fields syntax
The class fields syntax can be used to declare class properties directly on the class without the need for a constructor method. This syntax can be used in React classes to declare state and other properties.

Can I use web components in React application?

  • Yes, but it requires additional configuration
  • No, React does not support web components
  • Yes, web components can be used directly in React
  • Yes, but it requires using a separate library
Web components can be used in a React application, but it requires additional configuration to enable support for custom elements and shadow DOM. React components can also be wrapped in web components, allowing them to be used in non-React contexts.

What is the purpose of getDerivedStateFromError?

  • To handle errors that occur during rendering
  • To update the component's state based on the error that occurred
  • To provide additional information about the error to the user
  • To log the error to the console
The getDerivedStateFromError() method is a lifecycle method in React that is called whenever an error is thrown during rendering. Its main purpose is to update the component's state with information about the error that occurred, which can then be used to render an error message or to trigger some other action in the application.