reactjs - ReactComponent 在使用 laravel 的反应脚手架中不起作用
问题描述
所以我有这个代码
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App/index.js';
import 'bootstrap/dist/css/bootstrap.min.css';
import { ReactComponent as Logo } from './assets/logo.svg';
const Logoish = () => <Logo />
import * as serviceWorker from './serviceWorker';
ReactDOM.render(<Logoish />, document.getElementById('app'));
serviceWorker.unregister();
如果你愿意,那是我的 index.js。在过去的两周里,我试图让 react 和 laravel 一起工作,我得到的所有错误都是非常模糊React.createElement: type is invalid
的,我根本无法追踪(也许我在错误代码中遗漏了一些我不知道的东西)。
所以我从 laravel -> react -> react-router -> react-bootstrap -> components -> assets 追踪它,我意识到我的资产没有加载,因此整个应用程序都不会加载。我挖得更深,发现这ReactComponent
是负责任的。我将附上我在下面得到的错误代码。如果我这样导入:
import logo from './assets/logo.svg';
const Logoish = <img src={logo} alt="Logo" />
有用。但不是在上面的代码中:
import { ReactComponent as Logo } from './assets/logo.svg';
const Logoish = () => <Logo />
这是错误消息:
Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
Check the render method of `Logoish`.
in Logoish app.js:91655:32
warningWithoutStack http://localhost:8080/js/app.js:91655
warning http://localhost:8080/js/app.js:92126
createElementWithValidation http://localhost:8080/js/app.js:93523
Logoish http://localhost:8080/js/app.js:96841
renderWithHooks http://localhost:8080/js/app.js:74007
mountIndeterminateComponent http://localhost:8080/js/app.js:76541
beginWork$1 http://localhost:8080/js/app.js:77909
beginWork$$1 http://localhost:8080/js/app.js:83503
performUnitOfWork http://localhost:8080/js/app.js:82442
workLoopSync http://localhost:8080/js/app.js:82418
performSyncWorkOnRoot http://localhost:8080/js/app.js:82017
scheduleUpdateOnFiber http://localhost:8080/js/app.js:81445
updateContainer http://localhost:8080/js/app.js:84850
legacyRenderSubtreeIntoContainer http://localhost:8080/js/app.js:85275
unbatchedUpdates http://localhost:8080/js/app.js:82180
legacyRenderSubtreeIntoContainer http://localhost:8080/js/app.js:85274
render http://localhost:8080/js/app.js:85355
js http://localhost:8080/js/app.js:96845
__webpack_require__ http://localhost:8080/js/app.js:20
js http://localhost:8080/js/app.js:96763
__webpack_require__ http://localhost:8080/js/app.js:20
0 http://localhost:8080/js/app.js:98534
__webpack_require__ http://localhost:8080/js/app.js:20
<anonymous> http://localhost:8080/js/app.js:84
<anonymous> http://localhost:8080/js/app.js:87
参考这个 StackOverflow 线程,我猜它与 webpack 和 require 有关。
解决方案
推荐阅读
- c# - Depurar não disponível no Visual Studio - Visual Studio 中不提供调试选项
- c - 如何限制用户输入
- python - 为什么在 Kivy ScrollView 中将 minimum_height 绑定到 self.setter('height')
- apache-spark - 选择最佳超参数组合后,SparkML CrossValidator 是否会重新适应完整的训练数据集?
- django - django 连接过滤器模型
- java - Java是强类型的
- html - 如何停止嵌入 youtube 视频的声音?
- linux - 我可以在传输数据时指定linux内核套接字将用户数据复制到DMA内存吗?
- java - MVEL 验证没有值的算术表达式
- apache-spark-sql - 使用sql查询替换除零以外的所有数字