What is the difference between constructor and getInitialState?
- There is no difference, they both initialize component state
- constructor is called before getInitialState
- constructor is used in ES6 classes, while getInitialState is used in ES5 classes
- getInitialState is only used in functional components
The "constructor" method and "getInitialState" method are both used to initialize the state of a component in React, but they are used in different class styles. The "constructor" method is used in ES6 classes, while "getInitialState" is used in ES5 classes. In general, it is recommended to use the "constructor" method in modern React code.
What are Pure Components?
- Components that implement shouldComponentUpdate
- Components that only have a render method
- Components that use the React.Pure API
- Components that use the useMemo hook
Pure components in React are components that implement the shouldComponentUpdate lifecycle method to improve performance. This method compares the current props and state to the next props and state, and determines whether the component needs to be re-rendered. If the props and state have not changed, the component can skip the rendering process.
How Virtual DOM works?
- It creates a new DOM tree on each update
- It updates only the changed parts of the actual DOM
- It updates the Virtual DOM and calculates the most efficient way to update the actual DOM
- It updates the actual DOM directly
When a component's state changes in React, it updates the Virtual DOM and calculates the most efficient way to update the actual DOM based on the differences between the two versions. This approach minimizes the number of changes that need to be made to the actual DOM and improves performance.
How JSX prevents Injection Attacks?
- By using string concatenation
- By using a virtual DOM
- By escaping special characters
- By using JavaScript eval() function
JSX prevents injection attacks by escaping special characters. When JSX is compiled, special characters are automatically escaped, preventing them from being interpreted as code. This helps to prevent XSS (cross-site scripting) attacks.
How you use decorators in React?
- Use them as HOCs
- Use them as class decorators
- Use them as event handlers
- Use them as function decorators
Decorators in React can be used as class decorators to add functionality to a component class. For example, you can use a decorator to add additional lifecycle methods or state to a component. Decorators can be written as higher-order components (HOCs) or as regular functions.
How React PropTypes allow different type for one prop?
- PropTypes does not allow different types for one prop
- You need to define separate PropTypes for each type
- You can use PropTypes.oneOfType()
You can use PropTypes.oneOfType() to allow a prop to have multiple possible types. For example, you can define a prop that can be either a string or a number like this: 'myProp: PropTypes.oneOfType([PropTypes.string, PropTypes.number])'. This will allow the prop to accept values of either type, and will throw a warning if a value of a different type is passed in.
How to focus an input element on page load?
- Use the autoFocus attribute on the input element
- Use the focus() method in componentDidMount()
- Use the onFocus() event in the render method
- Use the onLoad() event on the body element
In React, you can focus an input element on page load by using the "focus()" method in the "componentDidMount()" lifecycle method. This will set the focus to the input element after the component has been mounted in the DOM. For example: componentDidMount() { this.myInput.focus(); }.
When component props defaults to true?
- When the prop is undefined
- When the prop is null
- When the prop is an empty string
- When the prop is zero
In React, a component's props will default to true if the prop value is undefined. This can happen if the prop is not passed explicitly in the component declaration or if it is explicitly set to undefined in the parent component. To avoid this behavior, default values can be set for props using the defaultProps property in the component class.
What is "key" prop and what is the benefit of using it in arrays of elements?
- A prop that determines the order of elements in an array
- A prop that provides a unique identifier for each element in an array
- A prop that specifies the position of an element in an array
- A prop that specifies the type of each element in an array
The "key" prop in React is a special prop that provides a unique identifier for each element in an array of elements. It is used to optimize the performance of rendering by allowing React to identify which elements have changed, and to update only those elements instead of re-rendering the entire list. The "key" prop should be a unique and stable identifier for each element, such as an ID or index.
How to pass params to history.push method in React Router v4?
- Pass a string with the route path and query parameters
- Pass an object with a "params" property
- Pass an object with a "query" property
- Pass an object with a "state" property
In React Router v4, you can pass params to the history.push method by using the "state" property of the location object. This property can contain any data that you want to pass along with the route, such as query parameters, form data, or session information. For example: this.props.history.push({ pathname: '/new-route', state: { foo: 'bar' } });.