簡體   English   中英

有沒有一種方法可以在沒有ES6類和Babel等預編譯工具的情況下創建React Components?

[英]Is there a way to create React Components without ES6 classes and pre compiling tools like Babel?

我開始學習React。 閱讀文檔后,似乎常規的組件創建方法是使用ES6類:

class Greeting extends React.Component {
  render() {
    return <h1>Hello, {this.props.name}</h1>;
  }
}

現在,我想知道是否有一種方法可以創建沒有ES6和預編譯工具(例如Babel)的組件。 React文檔建議您使用一個名為create-react-class的庫:

var createReactClass = require('create-react-class');
var Greeting = createReactClass({
  render: function() {
    return <h1>Hello, {this.props.name}</h1>;
  }
});

但這還要求您使用Node並最終編譯代碼。

我正在尋找的是一種僅使用ES5且完全沒有依賴性的方法。 那里的React.createClass`可以正常工作,但是畢竟不推薦使用。

另一種方法是僅使用普通函數,但是在這種情況下,我將不會獲得像componentDidMount這樣的鈎子的好處。

那么,有人對此有解決方案或建議嗎? 任何提示都將受到歡迎。

您不必使用節點,只需將CDN包含在瀏覽器中並使用createClass。

這是CDN:

<script crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>

檢出: 不帶ES6的React

編輯:正如@Sidney所說,您還將需要沒有JSX的React

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM