Home > Web Front-end > JS Tutorial > What is JSX? Introduction to how to use jsx (with code)

What is JSX? Introduction to how to use jsx (with code)

不言
Release: 2018-08-17 11:20:26
Original
6245 people have browsed it
What is jsx? What are the uses of jsx? What this article brings to you is about what is JSX? An introduction to how to use jsx (with code), let’s take a look at the specific content.

In react, our page content is written through JSX, so what exactly is JSX? JSX is actually a JavaScript object that creates a js object to describe the information of the HTML structure. Remember here that JSX is an extension language of js, similar to HTML but not HTML, because JSX can also perform calculations, judgments, add some js languages, etc.

Usage of jsx Operations in JSX

   render() {
      return(
        <div>
           <h2>算数题</h2>
           <ul>
             <li>5+6={5+6}</li>
             <li>6+6={6+6}</li>
             <li>10*10={10*10}</li>
           </ul>
        </div>
      )
    }
Copy after login

In JSX, {} is used to distinguish between HTML and js, that is Say, all js languages ​​must be enclosed in {}

Use of jsx variables in JSX

 render() {
      const flag = true;
      return(
        <div>
            {flag ? (<div>flag为真</div>) : (<div>flag为假</div>)}
        </div>
      )
    }
Copy after login

Use of jsx Styles in JSX

In JSX, adding styles to elements also uses the style attribute, but style contains a style object, as shown below:

 render() {
      var newStyle = {
        background: 'blue',
        fontSize:'15px'
      }
      return(
        <div>
            <div style={{color: &#39;red&#39;}}>颜色</div>
            <div style={newStyle}>样式</div>
        </div>
      )
    }
Copy after login

Through the above case, we can know that in JSX, the style attribute name must be named in camel case

HTML tags in JSX used in jsx

In JSX , some tag names need to be converted in order to prevent conflicts:

  1. class must use className to replace the for attribute in the

  2. lable element. Use htmlFor instead, as follows:

<label htmlFor="msg" ></label>
Copy after login

Also note here that all tags in JSX must be closing tags

Related recommendations:

How Vue supports JSX syntax detailed explanation

JSX syntax learning introductory tutorial in JavaScript's React framework_Basic knowledge

Detailed introduction to React


The above is the detailed content of What is JSX? Introduction to how to use jsx (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template