Hello, world;"; 2. Through "React.createElement(type, props, children)" Syntax for creating React elements."/> Hello, world;"; 2. Through "React.createElement(type, props, children)" Syntax for creating React elements.">
Home > Article > Web Front-end > What is the method to create elements in react
How to create elements in react: 1. Use JSX syntax to create React elements, with syntax such as "const element =
Hello, world
;"; 2. Through "React.createElement( type,props,children)" syntax to create React elements.
The operating environment of this tutorial: Windows 10 system, react18.0.0 version, Dell G3 computer.
What is the way to create elements in react?
Create react element
Rendering process: React element description virtual DOM, and then render the real DOM based on the virtual DOM.In general: Elements are pure objects used to describe DOM nodes or React components. Elements can contain other elements within their own attributes. The cost of creating an element is very low. Once an element is created, it never changes. For example: We use JSX syntax to create a React element elementVirtual DOM: It uses the js object structure to simulate the dom structure in html. For batch additions, deletions, modifications and queries, the js objects are directly manipulated first, and finally updated to the real DOM tree. Because directly operating js objects is faster than those APIs that operate DOM.
- React elements are js objects, which tell React what you want to display on the page.
const element = <h1 className='greeting'>Hello, world</h1>;During the compilation process, JSX will be compiled into a call to React.createElement(). The compiled result of the above example For:
const element = React.createElement( 'h1', {className: 'greeting'}, 'Hello, world!' );Finally, the value of element will be compiled into a js object similar to the following
const element = { type: 'h1', props: { className: 'greeting', children: 'Hello, world' }, _context: Object, _owner: null, key: null, ref: null, }
const element = <h1>Hello, world</h1>;
custom component: construction Components created by functions or components created by classes;
react native components: React.Fragment, etc.)
React.createElement(type,props,children).
React.createElement list, or can be written as multiple parameters:
<script type="text/babel"> const child1 = React.createElement("li",null,"one"); const child2 = React.createElement("li",null,"two"); const content = React.createElement("ul",{className:"testStyle"},[child1,child2]); ReactDOM.render( content, document.getElementById("example") ); //或者 const child1 = React.createElement("li",null,"one"); const child2 = React.createElement("li",null,"two"); const content = React.createElement("ul",{className:"testStyle"},child1,child2); ReactDOM.render( content, document.getElementById("example") ); </script>
const div = React.createElement('div', { id: 'box'}, 'test');console.log(div)recommends learning: "
The above is the detailed content of What is the method to create elements in react. For more information, please follow other related articles on the PHP Chinese website!