Real DOM: Real DOM updates slow. It can directly update HTML. Real DOM creates a new DOM if the element updates. DOM manipulation is very expensive. It is too much memory wastage.
Each React component must have a render () mandatorily. It returns a single React element which is the representation of the native DOM component. If more than one HTML element needs to be rendered, then they must be grouped together inside one enclosing tag such as, , etc. It must return the same result each time it is invoked.
Props are the shorthand for properties in React. They are read-only components that must be kept pure immutable. They are always passed down from the parent to the child components throughout the application. A child component can never send a prop back to the parent component. This helps in maintaining the unidirectional data flow and is generally used to render the dynamically generated data.
Arrow functions are more of a brief syntax for writing the function expression. They are also called ‘fat arrow’ ( => ) the functions. These functions allow to bind the context of the components property since in ES6 auto binging is not available by default.
In React everything is a component. Components are the building blocks of a React application’s UI. These components split up the entire UI into small independent are reusable pieces. Then it renders each of these components independent of each other without affecting the rest of the UI.
The state is the heart of React components. States are the source of data and must be kept as simple as possible. Basically, states are the objects which determine components rendering and behavior. They are mutable unlike the props and create dynamic and interactive components. They are accessed via this. state ().
Every component in React has lifecycle methods that we can tap into, to trigger changes at a particular phase of the cycle. Each component in react goes through three-phase: Mounting, Updating, and unmounting.
Some of the most important lifecycle methods are:
- componentWillMount() – Executed just before rendering takes place both on the client as well as server-side.
- componentDidMount() - Executed on the client-side only after the first render. 3.componentWillReceiveProps() - Invoked as soon as the props are received from the parent class and before another render is called.
- shouldComponentUpdate () - Returns true or false value based on certain conditions. If I want my component to update, return true else return false. By default, it returns false.
- componentWillUpdate() - called just before rendering takes place in the DOM.
- componentDidUpdate() - Called immediately after rendering takes place. 7.componentWillUnmount ()- called after the component is unmounted from the DOM. It is used to clear up the memory spaces.
React Hooks cannot be used in class components. They let us write components without class. Functional components were called stateless components. Only class components were used for state management and lifecycle methods. The need to change a functional component to a component, whenever state management or lifecycle methods were to be used, led to the development of Hooks.
Class components: Class components, on the other hand, have been around for quite some time. They use plain Java objects for creating pages. With the React create-a-class-factory method, a literal is passed in defining the method of a new component.