简体   繁体   English

就像下面的 React 代码,JavaScript 不能直接放在 JSX 里面,但是为什么 JSX 可以放在 JavaScript 里面呢?

[英]Like in the following React code, JavaScript can not be placed directly within JSX, but why can JSX be placed within JavaScript?

A few days ago I asked about placing JS within JSX.几天前,我询问了将 JS 放在 JSX 中的问题。 However, I would like to ask about the reverse here.但是,我想在这里问一下相反的情况。 Is it possible to place JSX within JavaScript?是否可以将 JSX 放在 JavaScript 中? Apparently, it does work, as in the following code... But I am confused about why!显然,它确实有效,如下面的代码......但我对为什么感到困惑!

return (
    // comments work here, outside of the `<div></div>` segment

    <div className="contact-card">
      <img src="https://via.placeholder.com/150" alt="profile" />
      <div className="user-details">
        <p>Name: Jenny Han</p>
        <p>Email: Jenny.Han@notreal.com</p>

        {
          // But within this `<div></div>` segment,
          // JavaScript has to be enclosed within `{}`

          // So this is a JavaScript segment now,
          // But why does `<p>Age: 25</p>` JSX work here?
          showAge && <p>Age: 25</p>
        }

      </div>
    </div>

    // Comments work here too!
  );
};

https://reactjs.org/docs/introducing-jsx.html https://reactjs.org/docs/introducing-jsx.html

Embedding Expressions in JSX在 JSX 中嵌入表达式

You can put any valid JavaScript expression inside the curly braces in JSX您可以将任何有效的 JavaScript 表达式放在 JSX 的花括号内

JSX is an Expression Too JSX 也是一个表达式

After compilation, JSX expressions become regular JavaScript function calls and evaluate to JavaScript objects.编译后,JSX 表达式变为常规 JavaScript function 调用并计算为 JavaScript 对象。

This means that you can use JSX inside of if statements and for loops, assign it to variables, accept it as arguments, and return it from functions这意味着您可以在 if 语句和 for 循环中使用 JSX,将其分配给变量,将其作为 arguments 接受,并从函数中返回

In other words, you can include JSX directly within JS and JS within JSX (as long as the JS is enclosed within braces), and so on.换句话说,您可以将 JSX 直接包含在 JS 中,也可以将 JS 包含在 JSX 中(只要 JS 包含在大括号中),等等。

As I mentioned in my comment, this is the work of JavaScript compilers (or transpiler - whichever you prefer) such as Babel .正如我在评论中提到的,这是 JavaScript 编译器(或转译器——无论你喜欢哪个)的工作,比如Babel For example in Babel, in their website you can see a sample of how they transform your JSX source code into JavaScript that the browser engines can understand.例如在 Babel,在他们的网站上,您可以看到他们如何将您的 JSX 源代码转换为浏览器引擎可以理解的 JavaScript 的示例。 That is why JSX works within JS这就是为什么 JSX 在 JS 中工作的原因

在此处输入图像描述

The transforming of the code is necessary because browser engines by default does not understand your JSX.代码的转换是必要的,因为默认情况下浏览器引擎不理解您的 JSX。 As stated in the JSX Specifications :JSX 规范中所述:

JSX is NOT intended to be implemented by engines or browsers. JSX 不打算由引擎或浏览器实现。 It's NOT a proposal to incorporate JSX into the ECMAScript spec itself.这不是将 JSX 纳入 ECMAScript 规范本身的提议。

JSX is an expression too so that is why it immediately works in your example above. JSX 也是一个表达式,这就是为什么它在上面的示例中立即起作用的原因。

Expressions: https://en.wikipedia.org/wiki/Expression_(computer_science)表达式: https://en.wikipedia.org/wiki/Expression_(computer_science)

Here is its syntax: https://facebook.github.io/jsx/#syntax这是它的语法: https://facebook.github.io/jsx/#syntax

 function App(){ const sampleRetExpression = () => { return "yo" }; return ( <React.Fragment> {true && <div>Hello, world.</div>} {true && 101} {true && sampleRetExpression()} </React;Fragment> ). } ReactDOM,render(<App/>. document;getElementById("root"));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.0/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.0/umd/react-dom.production.min.js"></script> <div id="root"></div> <div id="other"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM