Home Web Front-end JS Tutorial Know JSX principles in one article (recommended)

Know JSX principles in one article (recommended)

Jan 06, 2022 pm 03:38 PM
jsx

To understand the principles of JSX, you need to first understand how to use JavaScript objects to express the structure of a DOM element.
Look at the DOM structure below:

<div class=&#39;app&#39; id=&#39;appRoot&#39;>
  <h1 class=&#39;title&#39;>欢迎进入React的世界</h1>
  <p>
    React.js 是一个帮助你构建页面 UI 的库
  </p>
</div>
Copy after login

We can use JavaScript objects to represent all the information in the above HTML:

{
  tag: &#39;p&#39;,
  attrs: { className: &#39;app&#39;, id: &#39;appRoot&#39;},
  children: [
    {
      tag: &#39;h1&#39;,
      attrs: { className: &#39;title&#39; },
      children: [&#39;欢迎进入React的世界&#39;]
    },
    {
      tag: &#39;p&#39;,
      attrs: null,
      children: [&#39;React.js 是一个构建页面 UI 的库&#39;]
    }
  ]
}
Copy after login

But this is too long to write in JavaScript, and the structure It's not clear either, it's very convenient to use HTML.
So React.js expanded the syntax of JavaScript to allow writing syntax similar to HTML tag structures in JavaScript code, which is much more convenient. The compilation process will convert JSX structures similar to HTML into JavaScript objects. structure.

import React from &#39;react&#39;
import ReactDOM from &#39;react-dom&#39;

class App extends React.Component {
  render () {
    return (
      <p className=&#39;app&#39; id=&#39;appRoot&#39;>
        <h1 className=&#39;title&#39;>欢迎进入React的世界</h1>
        <p>
          React.js 是一个构建页面 UI 的库
        </p>
      </p>
    )
  }
}

ReactDOM.render(
    <App />,
  document.getElementById(&#39;root&#39;)
)
Copy after login

Convert to

import React from &#39;react&#39;
import ReactDOM from &#39;react-dom&#39;

class App extends React.Component {
  render () {
    return (
      React.createElement(
        "p",
        {
          className: &#39;app&#39;,
          id: &#39;appRoot&#39;
        },
        React.createElement(
          "h1",
          { className: &#39;title&#39; },
          "欢迎进入React的世界"
        ),
        React.createElement(
          "p",
          null,
          "React.js 是一个构建页面 UI 的库"
        )
      )
    )
  }
}

ReactDOM.render(
    React.createElement(App),
  document.getElementById(&#39;root&#39;)
)
Copy after login

React.createElement will construct a JavaScript object to describe your HTML structure information, including tag names, attributes, sub-elements, etc. The syntax is

React.createElement(
  type,
  [props],
  [...children]
)
Copy after login

The so-called JSX is actually a JavaScript object, so when using React and JSX, you must go through the compilation process

JSX — Use react to construct components, and babel compiles them—> JavaScript objects— ReactDOM .render() —> DOM element—> Insert page

Recommended learning: "JS Basic Tutorial"

The above is the detailed content of Know JSX principles in one article (recommended). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Let's talk about how Vue dynamically renders components through JSX Let's talk about how Vue dynamically renders components through JSX Dec 05, 2022 pm 06:52 PM

How does Vue dynamically render components through JSX? The following article will introduce to you how Vue can efficiently dynamically render components through JSX. I hope it will be helpful to you!

What is JSX in Vue? When to use it? how to use? What is JSX in Vue? When to use it? how to use? Jan 16, 2023 pm 08:23 PM

What is JSX in Vue? The following article will introduce you to JSX in Vue, introduce when to use JSX, and its basic use in Vue2. I hope it will be helpful to you!

Detailed explanation of how to use JSX in Vue3+Vite Detailed explanation of how to use JSX in Vue3+Vite Dec 09, 2022 pm 08:27 PM

How to use JSX in Vue+Vite? The following article will introduce to you how to use JSX in Vue3+Vite. I hope it will be helpful to you!

How to use jsx syntax in vue3 How to use jsx syntax in vue3 May 12, 2023 pm 12:46 PM

Background: The vue3 project promotes the use of composition-api+setup format combined with jsx+hooks format, separated by business, with clearer logic and easier maintenance. The syntax below mainly achieves the same function by comparing the different syntaxes of jsx and template. 1. Ordinary content. Ordinary content constants. The writing method is the same //jsx writing method setup(){return()=>hello}//tempalte writing method hello 2. Dynamic variables jsx uniformly uses curly brackets to wrap variables without quotation marks. For example, the content of {age}tempalte is wrapped in double curly brackets, and attribute variables start with a colon. For example, {{age}}{} is from jsx.

Vite creates Vue3 projects and how Vue3 uses jsx Vite creates Vue3 projects and how Vue3 uses jsx May 22, 2023 pm 01:58 PM

To create a Vue3 project with Vite, Vite requires Node.js version >= 12.0.0. (node-v to view your current node version) Use yarn: yarncreate@vitejs/app Use npm: npminit@vitejs/app1. Enter the project name. Enter our project name here: vite-vue32. Select the framework and select the one we need to integrate. Framework: vuevanilla: native js, no framework integrates vue: vue3 framework, only supports vue3react: react framework preact: lightweight react framework lit-element: lightweight we

How does Vue implement JSX syntax and component programming? How does Vue implement JSX syntax and component programming? Jun 27, 2023 am 11:48 AM

Vue is a popular front-end framework that provides a component-based development model. However, Vue does not natively support JSX syntax, but we can implement JSX syntax and component programming by using third-party libraries. 1. What is JSX? JSX is an extension syntax of JavaScript that can write HTML-like code in JavaScript. React is the first front-end framework to introduce JSX syntax. Using JSX syntax can make description more natural and convenient.

An article explaining in detail how to use JSX in vue3 An article explaining in detail how to use JSX in vue3 Nov 25, 2022 pm 09:01 PM

How to use JSX in vue? The following article will introduce to you how to use JSX in vue3. I hope it will be helpful to you!

How to use jsx/tsx in Vue3 How to use jsx/tsx in Vue3 May 11, 2023 pm 02:07 PM

How to use JSX Here we take the vite project as an example. To use JSX in the project, we need to install a plug-in @vitejs/plugin-vue-jsx. This plug-in allows us to use JSX/TSX in the project npmi@vitejs/plugin-vue - After the installation of -jsx-D is completed, make a configuration in vite.config.ts to import{defineConfig}from"vite";importvuefrom"@vitejs/plugin-vue";importvueJsxfrom"@

See all articles