【发布时间】:2021-02-25 21:57:43
【问题描述】:
我正在构建一个非常基本的 React 应用程序作为新项目的起点,并尝试集成 Mobx 和 Styled Components。我遇到的问题是,每当我尝试在我的反应树中包含一个样式组件或用 mobx observer 包装的反应组件时,我都会收到以下错误:
Uncaught Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.`
我已经更新到每个相关库的最新版本,并升级到 node 和 npm 的最新版本(目前分别为 15.2.0 和 7.0.8)。我已经遇到了一段时间(使用旧版本)并且完全不知道解决方案。我假设这与我的构建/捆绑过程有关,所以我在下面链接了相关文件。很高兴包含任何其他可能有用的文件。任何指针都非常感谢!
- Here is my package.json
- Here is my webpack config
- 这是我的应用程序的最简化版本,它会产生错误:
import ReactDOM from "react-dom"
import { observer } from "mobx-react"
import { makeAutoObservable } from "mobx"
class Store {
counter = 1
constructor() {
makeAutoObservable(this)
}
get counterPlusOne () {
return this.counter + 1
}
incrementCounter = () => {
this.counter++
}
}
const store = new Store()
// Removing `observer` on the next line removes the error but fails to integrate mobx
const App = observer(({store}) => {
return (
<div>
<p className="counter-state">Counter is at: {store.counter}</p>
<button onClick={store.incrementCounter}>
Increment counter
</button>
<p className="next-counter-state">
Clicking the button will set the counter to: {store.counterPlusOne}
</p>
</div>
)
})
ReactDOM.render(
<App store={store} />,
document.getElementById("root")
)
另外值得注意的是:我对这个错误的谷歌搜索主要表明存在导入问题,但我相当确定这不是本例中的问题。
【问题讨论】:
标签: reactjs babeljs styled-components mobx mobx-react