Question:- What is a State in React?
Answer:- The State is an updatable structure which holds the data and information about the component. It may be changed over the lifetime of the component in response to user action or system event. It is the heart of the react component which determines the behavior of the component and how it will render. It must be kept as simple as possible.
Question:- How can you update the State of a component?
Answer:- We can update the State of a component using this.setState() method. This method does not always replace the State immediately. Instead, it only adds changes to the original State. It is a primary method which is used to update the user interface(UI) in response to event handlers and server responses.
Question:- What is arrow function in React? How is it used?
Answer:- The Arrow function is the new feature of the ES6 standard. If you need to use arrow functions, it is not necessary to bind any event to this. Here, the scope of this is global and not limited to any calling function. So If you are using Arrow Function, there is no need to bind this inside the constructor. It is also called fat arrow (=>) functions.
Question:- What is an event in React?
Answer:- An event is an action which triggers as a result of the user action or system generated event like a mouse click, loading of a web page, pressing a key, window resizes, etc. In React, the event handling system is very similar to handling events in DOM elements. The React event handling system is known as Synthetic Event, which is a cross-browser wrapper of the browsers native event. Handling events with React have some syntactical differences, which are: - React events are named as camelCase instead of lowercase. - With JSX, a function is passed as the event handler instead of a string.
Question:- What are synthetic events in React?
Answer:- A synthetic event is an object which acts as a cross-browser wrapper around the browsers native event. It combines the behavior of different browsers native event into one API, including stopPropagation() and preventDefault().
Question:- Explain the Lists in React.
Answer:- Lists are used to display data in an ordered format. In React, Lists can be created in a similar way as we create it in JavaScript. We can traverse the elements of the list using the map() function.
Question:- What is the significance of keys in React?
Answer:- A key is a unique identifier. In React, it is used to identify which items have changed, updated, or deleted from the Lists. It is useful when we dynamically created components or when the users alter the lists. It also helps to determine which components in a collection needs to be re-rendered instead of re-rendering the entire set of components every time. It increases application performance.
Question:- How are forms created in React?
Answer:- Forms allow the users to interact with the application as well as gather information from the users. Forms can perform many tasks such as user authentication, adding user, searching, filtering, etc. A form can contain text fields, buttons, checkbox, radio button, etc. React offers a stateful, reactive approach to build a form. The forms in React are similar to HTML forms. But in React, the state property of the component is only updated via setState(), and a JavaScript function handles their submission. This function has full access to the data which is entered by the user into a form.
Question:- What are the different phases of React components lifecycle?
Answer:- The different phases of React components lifecycle are: - Initial Phase: It is the birth phase of the React lifecycle when the component starts its journey on a way to the DOM. In this phase, a component contains the default Props and initial State. These default properties are done in the constructor of a component. - Mounting Phase: In this phase, the instance of a component is created and added into the DOM. - Updating Phase: It is the next phase of the React lifecycle. In this phase, we get new Props and change State. This phase can potentially update and re-render only when a prop or state change occurs. The main aim of this phase is to ensure that the component is displaying the latest version of itself. This phase repeats again and again. - Unmounting Phase: It is the final phase of the React lifecycle, where the component instance is destroyed and unmounted(removed) from the DOM.
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.
