升级到create-react-app 4.0版本时,使用react-app-rewired出现了服务器启动错误

3

我在网上搜寻了很久,但没有找到关于这个情况的提及,尽管它肯定不罕见。

我一直使用 create-react-app(版本为 3.4.x),并且与 react-app-rewired 结合使用 [主要是为了启用 MobX 的装饰器支持而不需要弹出].

最近,我尝试按照指示升级 cra 到最新版本(4.0),运行以下命令:

yarn add --exact react-scripts@4.0.0

然而,现在启动React服务器时,我遇到了以下错误:

yarn start
yarn run v1.22.5
$ HTTPS=true BROWSER=none react-app-rewired start --env=local
Cannot read property 'use' of undefined
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

如果我删除react-app-rewired并将启动脚本改回使用react-scripts,服务器会启动,但我不再有装饰器支持。
问题: react-app-rewired是否支持 4.0?有没有其他替代方案可以在不弹出的情况下启用装饰器?感谢任何意见!

嗨 @mjh!你找到解决服务器启动错误的方法了吗? - Blueraaga
这个问题有任何其他更新吗? - Sajjad Shahi
1个回答

1

我不知道关于rewired的答案,但是在MobX 6中有一个新功能,可能让你完全摆脱装饰器,makeAutoObservable

import { makeAutoObservable } from "mobx"

class Store {
  // Don't need decorators now
  string = 'Test String';

  setString = (string) => {
    this.string = string;
  };

  constructor() {
    // Just call it here
    makeAutoObservable (this);
  }
}

更多信息在这里 https://mobx.js.org/migrating-from-4-or-5.htmlhttps://mobx.js.org/react-integration.html


谢谢,这很有帮助,也许这是我们必须走的方向。虽然我认为装饰器很酷。 - mjh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接