gpt4 book ai didi

javascript - 无法在没有装饰器支持的情况下使类成为 mobx 观察者 - "TypeError Cannot read property ' 未定义的观察者”

转载 作者:行者123 更新时间:2023-11-30 19:46:23 25 4
gpt4 key购买 nike

这是index.js文件

import React from "react";
import ReactDOM from "react-dom";
import mobx, { observable, action, decorate } from "mobx";
import mobxReact from "mobx-react";

import "./styles.css";

const Main = mobxReact.observer(
class Main extends React.Component {
// @observable internalState = false;
constructor(props) {
super(props);
this.internalState = false;
}

toggleMenu = () => {
console.log("Toggle button");
this.internalState = !this.internalState;
};

render() {
return (
<div className="App">
<h1>Toggle State</h1>
<input type="button" onClick={this.toggleMenu} value="Toggle" />
<div style={{ paddingTop: "25px" }}>
internalState {this.internalState.toString()}{" "}
</div>
</div>
);
}
}
);

decorate(Main, {
internalState: observable,
toggleMenu: action
});

function App() {
return <Main />;
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

这是 codesandbox 代码的链接:https://codesandbox.io/s/k37v35vylv

由于这个codesandbox的模板不支持decorator语法,我试图让Main类成为一个observer 没有 @observer

我收到这个错误:

enter image description here

错误文本:

TypeError: Cannot read property 'observer' of undefined

依赖关系:

enter image description here

最佳答案

mobx-react 包中没有默认导出。所以我建议你可以这样编码:从 'mobx-react' 导入 {observer} enter image description here

关于javascript - 无法在没有装饰器支持的情况下使类成为 mobx 观察者 - "TypeError Cannot read property ' 未定义的观察者”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54901090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com