What is the difference between Shadow DOM and Virtual DOM?
- Shadow DOM is a browser feature, while Virtual DOM is a React feature
- Shadow DOM is used for server-side rendering, while Virtual DOM is used for client-side rendering
- Shadow DOM is used for styling and encapsulation, while Virtual DOM is used for performance optimization
- There is no difference
Shadow DOM and Virtual DOM are two different concepts. Shadow DOM is a browser feature that allows developers to encapsulate styles and markup within components. Virtual DOM, on the other hand, is a React feature that allows for performance optimization by minimizing changes to the actual DOM.
Why should we not update the state directly?
- It can cause memory leaks
- It can cause race conditions
- It can cause the application to crash
- It can cause the component to re-render
In React, state should not be updated directly because it can cause the component to re-render improperly. Instead, the setState method should be used to update state, which triggers a re-render of the component and ensures that the updated state is properly reflected in the UI.
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 purpose of displayName class property?
- To set the name of the component's file
- To set the name of the component for debugging purposes
- To specify the component's DOM display style
- To set the component's initial state
The displayName class property in a React component is used to set the name of the component for debugging purposes. This name is used in error messages and in the React Developer Tools to help identify the component in the component hierarchy. If the displayName property is not set explicitly, React will attempt to infer the name of the component from the name of the component class or function.
What is state in React?
- A component's HTML markup
- A component's internal data
- A component's lifecycle methods
- A component's properties
In React, state refers to a component's internal data that can change over time. State is managed using the setState method, which allows developers to update the state of a component and trigger a re-render of the UI. State is used to keep track of data that changes in response to user input or other events.