React Flashcards
Learn react topics
React’s Virtual DOM
React’s virtual DOM implies a “virtual” representation (as a tree, as each element is a node that holds an object ) of a user interface, which is preserved in memory and synchronized with the browser’s DOM via React’s ReactDOM library.
What is DOM?
When a webpage is loaded into a browser, the browser typically receives an HTML document for that page from the server. The browser constructs a logical, tree-like structure from the HTML to show the requested page to the client. The DOM refers to this tree structure.
The Document Object Model (DOM) is a logical tree that describes a document. Each tree branch terminates in a node, which holds an object. Because the document on the browser has been parsed to a tree structure, methods that offer programmatic access to the tree, allowing one to change the structure, style, or content of a document, are required. This gave rise to the DOM API, which provides these methods for altering the elements represented as nodes in the tree.
What is JSX?
JSX is a syntax extension for JavaScript that lets you write HTML-like markup inside a JavaScript file.
constructor()
The constructor() method is called when the component is first created. You use it to initialize the component’s state and bind methods to the component’s instance
render()
The render() method is responsible for generating the component’s virtual DOM representation based on its current props and state. It is called every time the component needs to be re-rendered, either because its props or state have changed, or because a parent component has been re-rendered.
getDerivedStateFromProps()
getDerivedStateFromProps() is a lifecycle method available in React 16.3 and later versions that is invoked during the mounting and updating phase of a component.
During the mounting phase, getDerivedStateFromProps() is called after the constructor and before render(). This method is called for every render cycle and provides an opportunity to update the component’s state based on changes in props before the initial render.
componentDidMount()
The componentDidMount() method is called once the component has been mounted into the DOM. It is typically used to set up any necessary event listeners or timers, perform any necessary API calls or data fetching, and perform other initialization tasks that require access to the browser’s DOM API.
shouldComponentUpdate()
The shouldComponentUpdate() method is called before a component is updated. It takes two arguments: nextProps and nextState. This method returns a boolean value that determines whether the component should update or not. If this method returns true, the component will update, and if it returns false, the component will not update.
componentWillUpdate()
componentWillUpdate() is a lifecycle method in React that gets called just before a component’s update cycle starts. It receives the next prop and state as arguments and allows you to perform any necessary actions before the component updates.
componentDidUpdate()
The componentDidUpdate() method is a lifecycle method in React that is called after a component has been updated and re-rendered. It is useful for performing side effects or additional operations when the component’s props or state have changed.
getSnapshotBeforeUpdate()
The getSnapshotBeforeUpdate() method is called just before the component’s UI is updated. It allows the component to capture some information about the current state of the UI, such as the scroll position before it changes. This method returns a value that is passed as the third parameter to the componentDidUpdate() method.
componentWillUnmount()
During the unmounting phase, React calls the following lifecycle methods in order:
componentWillUnmount(): This method is called just before the component is removed from the DOM. It allows you to perform any necessary cleanup, such as canceling timers, removing event listeners, or clearing any data structures that were set up during the mounting phase.
After componentWillUnmount() is called, the component is removed from the DOM and all of its state and props are destroyed.
What is the state in React?
State is a fundamental concept in React that allows you to store and manage data that can change over time. It represents the current state of a component and influences what is displayed on the user interface. State is essential for building dynamic and interactive web applications.
What are Props in React?
Props, short for properties, are a way to pass data from a parent component to a child component. They are read-only and help make your React components reusable and composable.
Composition x Inheritance
It is recommended to always use composition instead of inheritance