

How React works behind the scenes
source link: https://dev.to/amrtcrypto/how-react-works-behind-the-scenes-5e4k
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How React works behind the scenes
React is a popular JavaScript library for building user interfaces. It's declarative, efficient, and flexible.
But have you ever wondered how React actually works behind the scenes? Let's take a look at how React operates on a fundamental level.
React is a JavaScript library that creates and maintains an in-memory representation of a UI. When a user interacts with a React application, React updates the in-memory representation and the changes are reflected in the UI.
React uses a virtual DOM, which is a JavaScript representation of the actual DOM. The virtual DOM is a lightweight tree that contains only the necessary information to render a UI.
When a user interacts with a React application, React updates the virtual DOM. React then compares the virtual DOM with the actual DOM. React makes the necessary changes to the actual DOM, which are reflected in the UI.
The virtual DOM is a JavaScript object. The object is made up of React elements. React elements are the smallest units of React applications.
React elements are immutable. That means they cannot be changed. Once a React element is created, it cannot be modified.
This is why React is so fast. React only needs to update the changed element in the virtual DOM. React doesn't need to re-render the entire UI.
React elements are also lightweight. React elements are just JavaScript objects. They don't have any additional overhead.
React elements can be created with the React.createElement()
method.
React.createElement(
type,
props,
children
);
The type argument specifies the type of element to create. The type can be a HTML tag, a React component, or a function.
The props argument specifies the element's properties. The props are an object of key-value pairs.
The children argument specifies the element's children. The children can be React elements or strings.
Here's an example of creating a React element:
const element = React.createElement(
'div',
{ id: 'container' },
'Hello, world!'
);
The above code creates a React element of type 'div'. The element has an id property of 'container' and a child of 'Hello, world!'.
React elements are just JavaScript objects. They can be stored in variables and passed around like any other object.
React components are functions or classes that return React elements.
function Welcome(props) {
return React.createElement(
'h1',
null,
'Hello, ' + props.name + '!'
);
}
class Welcome extends React.Component {
render() {
return React.createElement(
'h1',
null,
'Hello, ' + this.props.name + '!'
);
}
}
The above code defines a Welcome component. The Welcome component returns a React element.
When a React element is created, React assigns a key to the element. The key is used to uniquely identify the element.
The key is used by React to keep track of the element's position in the virtual DOM. When a user interacts with a React application, React uses the key to quickly find the changed element in the virtual DOM.
React only updates the changed element in the virtual DOM and it doesn't need to re-render the entire UI.
The key is also used by React to figure out which DOM nodes to update. React uses the key to match the virtual DOM node with the actual DOM node.
React uses a diffing algorithm to find the changed element. The diffing algorithm is a clever way to figure out which element has changed.
React updates the actual DOM by making changes to the DOM nodes and only updates the changed DOM nodes.
Recommend
-
82
Pixel users get an exclusive preview of a new behind-the-scenes VR experience of Wes Anderson’s upcoming film “Isle of Dogs.”
-
40
Behind the scenes with the Dragon Ball Legends GCP backend...
-
22
The recently released Excelsior JET 15.3 has introduced out-of-the-box support for Spring Boot applications. Turning your Spring Boot jar into an optimized native binary has become as easy as adding a few lines to yourpo...
-
45
Overview of how does CSS works behind the scenes? Manisha Basra
-
44
If you have code on GitHub, chances are that you’ve had a security vulnerability alert at some point. Since the feature launched, GitHub has sent more than 62 million security alerts for vulnerable dependencies.
-
45
无法访问此网站 medium.com 拒绝了我们的连接请求。 ...
-
9
BH USA 2019 Behind the scenesSkip to content Another good run at BlackHat came to an end. I always wanted to write about what goes behind the scenes. Thi...
-
10
Behind the Scenes with Infinite RedWhat it’s like to be an Infinite Red client
-
7
Python behind the scenes #11: how the Python import system works Published: Tue 13 July 20...
-
1
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK