如何在Soft.io中添加一个React组件

huangapple go评论97阅读模式
英文:

How to add a react component in Soft.io

问题

I'm facing a problem where I'm trying to add a React component or recode it inline HTML in softr.io and I need help, if there is any way to do it.









英文:

im facing a problem where im trying to add a react component or recode it inline html in softr.io and i need help, if there is anyway to do it.

 <div id="root"></div>

<!-- Load React. -->
<!-- Note: when deploying, replace "development.js" with "production.min.js". -->
<script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>

<!-- Load your React component. -->
<script src="states_clock.js"></script>

<!-- Example script to create a React component and render it. -->
<script>
  // Define your React component.
  function StatesClock() {
    return <div>Hello, React!</div>;
  }

  // Render the component in the root element.
  ReactDOM.render(<StatesClock />, document.getElementById('root'));
</script>

</body>

答案1

得分: 1

Okay, so defining JSX inside of a script tag is impossible if you do not use some sort of compiler such as babel which can "understand" JSX and compile it into regular JavaScript for the browser to understand.

First, you'll need to include babel as a script in your HTML:

<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script>

Then you'll need to set the script type attribute to "text/babel":

<script type="text/babel">

Here is the complete, working example based on the code you provided:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
  <title>Test</title>

  <base href="/" />

  <meta name="color-scheme" content="light" />
  <meta name="viewport" content="viewport-fit=cover, width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  <meta name="format-detection" content="telephone=no" />
  <meta name="msapplication-tap-highlight" content="no" />

  <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />

  <link rel="shortcut icon" type="image/png" href="%PUBLIC_URL%/assets/images/logo.png" />

  <!-- add to homescreen for ios -->
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-title" content="Ionic App" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
</head>

<body>
  <div id="root"></div>
</body>
<script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script>
<script src="states_clock.js"></script>
<script type="text/babel">
  // Your React code here
  function StatesClock() {
    return <div>Hello, React!</div>;
  }

  ReactDOM.render(<StatesClock />, document.getElementById('root'));
</script>
</html>

<!-- end snippet -->

Note the console log, it is warning you that you are using [deprecated rendering code][1]. You should use [createRoot][2] instead.

[1]: https://react.dev/reference/react-dom#deprecated-apis
[2]: https://react.dev/reference/react-dom/client/createRoot


<details>
<summary>英文:</summary>

Okay, so defining `JSX` inside of a script tag is impossible if you do not use some sort of compiler such as `babel` which can &quot;understand&quot; `JSX` and compile it into regular `JavaScript` for the browser to understand.

First, you&#39;ll need to include babel as a script in your HTML:

    &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js&quot;&gt;&lt;/script&gt;

Then you&#39;ll need to set the script `type` attribute to &quot;text/babel&quot;:

    &lt;script type=&quot;text/babel&quot;&gt;

Here is the complete, working example based on the code you provided:

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-html --&gt;

    &lt;!DOCTYPE html&gt;
    &lt;html lang=&quot;en&quot;&gt;

    &lt;head&gt;
      &lt;meta charset=&quot;utf-8&quot; /&gt;
      &lt;title&gt;Test&lt;/title&gt;

      &lt;base href=&quot;/&quot; /&gt;

      &lt;meta name=&quot;color-scheme&quot; content=&quot;light&quot; /&gt;
      &lt;meta name=&quot;viewport&quot; content=&quot;viewport-fit=cover, width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no&quot; /&gt;
      &lt;meta name=&quot;format-detection&quot; content=&quot;telephone=no&quot; /&gt;
      &lt;meta name=&quot;msapplication-tap-highlight&quot; content=&quot;no&quot; /&gt;

      &lt;link rel=&quot;manifest&quot; href=&quot;%PUBLIC_URL%/manifest.json&quot; /&gt;

      &lt;link rel=&quot;shortcut icon&quot; type=&quot;image/png&quot; href=&quot;%PUBLIC_URL%/assets/images/logo.png&quot; /&gt;

      &lt;!-- add to homescreen for ios --&gt;
      &lt;meta name=&quot;apple-mobile-web-app-capable&quot; content=&quot;yes&quot; /&gt;
      &lt;meta name=&quot;apple-mobile-web-app-title&quot; content=&quot;Ionic App&quot; /&gt;
      &lt;meta name=&quot;apple-mobile-web-app-status-bar-style&quot; content=&quot;black&quot; /&gt;
    &lt;/head&gt;

    &lt;body&gt;
      &lt;div id=&quot;root&quot;&gt;&lt;/div&gt;
    &lt;/body&gt;
    &lt;script src=&quot;https://unpkg.com/react@18/umd/react.development.js&quot; crossorigin&gt;&lt;/script&gt;
    &lt;script src=&quot;https://unpkg.com/react-dom@18/umd/react-dom.development.js&quot; crossorigin&gt;&lt;/script&gt;
    &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;states_clock.js&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/babel&quot;&gt;
      // Your React code here
      function StatesClock() {
        return &lt;div&gt;Hello, React!&lt;/div&gt;;
      }

      ReactDOM.render(&lt;StatesClock /&gt;, document.getElementById(&#39;root&#39;));
    &lt;/script&gt;
    &lt;/html&gt;

&lt;!-- end snippet --&gt;

Note the console log, it is warning you that you are using [deprecated rendering code][1]. You should use [createRoot][2] instead.


  [1]: https://react.dev/reference/react-dom#deprecated-apis
  [2]: https://react.dev/reference/react-dom/client/createRoot

</details>



huangapple
  • 本文由 发表于 2023年8月10日 18:41:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76874968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定