javascript - 如何在单独的 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”?
解决方案
如果你是在你的containsindex.js
中加载的,那意味着你没有编译你的代码。浏览器本身不支持。html
<MainPage />
text/jsx
为了与 JSX 一起使用 react,您需要使用像webpack这样的编译器/捆绑器。
推荐阅读
- r - 如何在 R 中绘制分类模式?
- sql - DBT 云服务器错误中的错误:rpc 请求中的数据库错误(来自远程系统)语法错误在“)”处或附近
- aws-amplify - 我可以将哪些选项传递给 Amplify.configure()?
- android-studio - Kotlin KMM 'commonMain' 依赖项:运行正常,AndroidStudio 显示错误
- python - 滚动平均成本
- azure - 使用自签名证书进行 Azure 服务主体身份验证的风险?
- r - 如何根据 R Plotly 中的时间线创建堆叠条形图?
- node.js - 有没有办法将使用 window.print() 从前端生成的 PDF 发送到后端?
- javascript - 将输入表单电子邮件转换为小写但最后一个字母不转换?
- java - 通过回调方法使用 Quarkus/Mutiny 将 MyRequestService 连接到 Reactive REST GET 端点