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 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.

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.

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 do you apply vendor prefixes to inline styles in React?

  • Use the "autoprefixer" library with CSS-in-JS
  • Use the "postcss" library with a custom plugin
  • Use the "react-prefixer" library with inline styles
  • Use the "react-style-prefixer" library with inline styles
In React, you can apply vendor prefixes to inline styles by using the "react-style-prefixer" library. This library automatically adds vendor prefixes to CSS properties based on the browser's user agent, and can be used with inline styles or CSS-in-JS solutions like styled-components or emotion.

How do you use contextType?

  • By passing the context value as a prop
  • By using the useContext() hook
  • By assigning the contextType property in the class definition
  • By creating a context consumer
The contextType property is used to consume a context value in a class component in React. To use contextType, you assign the context object to the contextType property in the class definition. This allows the component to access the context value using the this.context property. ContextType can only be used with a single context object and can only be used in class components.

How to write comments in React?

  • By using the /* */ syntax
  • By using the // syntax
  • By using the syntax
  • By using the {/* */} syntax
In React, comments can be written using the {/* */} syntax. This syntax allows comments to be included directly in JSX code without causing syntax errors. Comments can be used to provide additional information or documentation about components, or to temporarily disable parts of the code for debugging purposes.

How to use styles in React?

  • Use the CSS module
  • Use the className attribute
  • Use the inline-style attribute
  • Use the style attribute
In React, you can use the style attribute to apply styles to a component. The style attribute takes an object that contains CSS properties and values, similar to inline styles in HTML.

How to use React label element?

In React, you can use the standard HTML "label" element to associate a label with an input field or other form element. To do this, you can use the "for" attribute on the label element, and set it to the "id" of the input field. In JSX, you can use the "htmlFor" attribute instead of "for", because "for" is a reserved keyword in JavaScript.

Give an example of reselect usage?

  • A selector that returns the length of an array
  • A selector that filters and sorts data from the Redux store
  • A selector that computes the sum of two numbers
An example of reselect usage is a selector that filters and sorts data from the Redux store. This selector can take other selectors as input and use them to filter and sort the data before returning it. For example, you could create a selector that filters a list of items by a certain category and then sorts the results by price. This selector would only recompute when the input selectors (category and items) have changed, which can help to improve performance.

How do you set a default value for an uncontrolled component?

  • Use the value attribute
  • Use the defaultValue attribute
  • Use the setState() method
  • Use the props attribute
In React, uncontrolled components are form components that store their own state internally. To set a default value for an uncontrolled component, you can use the defaultValue attribute. This will set the initial value of the component when it first renders.

Why are inline ref callbacks or functions not recommended?

  • They can cause memory leaks
  • They can introduce performance issues
  • They are not compatible with all browsers
  • They can interfere with the component lifecycle
Inline ref callbacks or functions are not recommended because they can cause memory leaks in your application. When using an inline ref callback, the function is recreated on each render, which can result in a buildup of references that are not properly cleaned up by the garbage collector. This can lead to memory leaks over time. It is recommended to use the createRef() API or a callback ref defined outside of the component to avoid these issues.