Question:- Explain the lifecycle methods of React components in detail.
Answer:- The important React lifecycle methods are: - getInitialState(): It is used to specify the default value of this.state. It is executed before the creation of the component. - componentWillMount(): It is executed before a component gets rendered into the DOM. - componentDidMount(): It is executed when the component gets rendered and placed on the DOM. Now, you can do any DOM querying operations. - componentWillReceiveProps(): It is invoked when a component receives new props from the parent class and before another render is called. If you want to update the State in response to prop changes, you should compare this.props and nextProps to perform State transition by using this.setState() method. - shouldComponentUpdate(): It is invoked when a component decides any changes/updation to the DOM and returns true or false value based on certain conditions. If this method returns true, the component will update. Otherwise, the component will skip the updating. - componentWillUpdate(): It is invoked before rendering takes place in the DOM. Here, you cant change the component State by invoking this.setState() method. It will not be called, if shouldComponentUpdate() returns false. - componentDidUpdate(): It is invoked immediately after rendering takes place. In this method, you can put any code inside this which you want to execute once the updating occurs. - componentWillUnmount(): It is invoked immediately before a component is destroyed and unmounted permanently. It is used to clear up the memory spaces such as invalidating timers, event listener, canceling network requests, or cleaning up DOM elements. If a component instance is unmounted, you cannot mount it again.
Question:- What are Pure Components?
Answer:- Pure components introduced in React 15.3 version. The React.Component and React.PureComponent differ in the shouldComponentUpdate() React lifecycle method. This method decides the re-rendering of the component by returning a boolean value (true or false). In React.Component, shouldComponentUpdate() method returns true by default. But in React.PureComponent, it compares the changes in state or props to re-render the component. The pure component enhances the simplicity of the code and performance of the application.
Question:- What are Higher Order Components(HOC)?
Answer:- In React, Higher Order Component is an advanced technique for reusing component logic. It is a function that takes a component and returns a new component. In other words, it is a function which accepts another function as an argument. According to the official website, it is not the feature(part) in React API, but a pattern that emerges from Reacts compositional nature.
Question:- What can you do with HOC?
Answer:- You can do many tasks with HOC, some of them are given below: - Code Reusability - Props manipulation - State manipulation - Render highjacking
Question:- Why is it necessary to start component names with a capital letter?
Answer:- In React, it is necessary to start component names with a capital letter. If we start the component name with lower case, it will throw an error as an unrecognized tag. It is because, in JSX, lower case tag names are considered as HTML tags.
Question:- What are fragments?
Answer:- In was introduced in React 16.2 version. In React, Fragments are used for components to return multiple elements. It allows you to group a list of multiple children without adding an extra node to the DOM.
Question:- Why are fragments better than container divs?
Answer:- - Fragments are faster and consume less memory because it did not create an extra DOM node. - Some CSS styling like CSS Grid and Flexbox have a special parent-child relationship and add
tags in the middle, which makes it hard to keep the desired layout. - The DOM Inspector is less cluttered.
Question:- How to apply validation on props in React?
Answer:- Props validation is a tool which helps the developers to avoid future bugs and problems. It makes your code more readable. React components used special property PropTypes that help you to catch bugs by validating data types of values passed through props, although it is not necessary to define components with propTypes. We can apply validation on props using App.propTypes in React component. When some of the props are passed with an invalid type, you will get the warnings on JavaScript console. After specifying the validation patterns, you need to set the App.defaultProps.
Question:- What is create-react-app?
Answer:- Create React App is a tool introduced by Facebook to build React applications. It provides you to create single-page React applications. The create-react-app are preconfigured, which saves you from time-consuming setup and configuration like Webpack or Babel. You need to run a single command to start the React project, which is given below. $ npx create-react-app my-app This command includes everything which we need to build a React app. Some of them are given below: - It includes React, JSX, ES6, and Flow syntax support. - It includes Autoprefixed CSS, so you dont need -webkit- or other prefixes. - It includes a fast, interactive unit test runner with built-in support for coverage reporting. - It includes a live development server that warns about common mistakes. - It includes a build script to bundle JS, CSS, and images for production, with hashes and source maps.
Question:- What is create-react-app?
Answer:- Create React App is a tool introduced by Facebook to build React applications. It provides you to create single-page React applications. The create-react-app are preconfigured, which saves you from time-consuming setup and configuration like Webpack or Babel. You need to run a single command to start the React project, which is given below. $ npx create-react-app my-app This command includes everything which we need to build a React app. Some of them are given below: - It includes React, JSX, ES6, and Flow syntax support. - It includes Autoprefixed CSS, so you dont need -webkit- or other prefixes. - It includes a fast, interactive unit test runner with built-in support for coverage reporting. - It includes a live development server that warns about common mistakes. - It includes a build script to bundle JS, CSS, and images for production, with hashes and source maps.
Question:- When do we prefer to use a class component over a function component?
Answer:- If a component needs state or lifecycle methods, we should use the class component; otherwise, use the function component. However, after React 16.8, with the addition of Hooks, you could use state, lifecycle methods, and other features that were only available in the class component right in your function component.
Question:- Is it possible for a web browser to read JSX directly?
Answer:- Web browsers cant read JSX directly. This is because the web browsers are built to read the regular JS objects only, and JSX is not a regular JavaScript object. If you want a web browser to read a JSX file, you must transform the files into a regular JavaScript object. For this purpose, Babel is used.
Question:- What do you understand by the state in React?
Answer:- In react, the state of a component is an object that holds some information that may change over the components lifetime. It would be best to try to make your state as simple as possible and minimize the number of stateful components.
Question:- What do you understand by props in React?
Answer:- In React, the props are inputs to components. They are single values or objects containing a set of values passed to components on creation using a naming convention similar to HTML-tag attributes. They are data passed down from a parent component to a child component. The main purpose of props in React is to provide the following component functionality: - Pass custom data to your component. - Trigger state changes. - Use via this.props.reactProp inside components render() method.
