首页 > 解决方案 > 如何在单独的 React 组件脚本中使用反应库?

问题描述

我正在尝试从 html 页面呈现反应组件。我引用了问题React - component in separate script does not work并从http://localhost/index.html之类的 URL 加载了 html 文件。我的 index.html:

<!DOCTYPE html>
   <html>
       <head>
           <script src="https://...fb.me/react-0.13.3.js"></script>
           <script src="https://...fb.me/JSXTransformer-0.13.3.js"></script>
       </head>
       <body>
           <div id="example"></div>
           <script type="text/jsx" src="index.js"></script>
       </body>
   </html>

我的 index.js:

 ReactDOM.render(<MainPage />, document.getElementById('example'))

错误是未定义 MainPage。但是当我写

include MainPage

包含语句有错误。另外,我如何从 react-tabs 中包含其他 React 库,例如“Tabs”?

标签: javascripthtmlreactjs

解决方案


如果你是在你的containsindex.js中加载的,那意味着你没有编译你的代码。浏览器本身不支持。html<MainPage />text/jsx

为了与 JSX 一起使用 react,您需要使用像webpack这样的编译器/捆绑器。


推荐阅读