undefined 不是一个函数(评估 'decorator(target, property, desc)')

2023-12-30

我想整合mobx https://mobx.js.org/ and mobx-坚持 https://github.com/pinqy520/mobx-persist with 反应导航 https://reactnavigation.org/.
我读过这些文章:

[1] https://hackernoon.com/react-navigation-with-mobx-2064fcdaa25b https://hackernoon.com/react-navigation-with-mobx-2064fcdaa25b
[2] https://blog.callstack.io/write-react-native-apps-in-2017-style-with-mobx-e2dffc209fcb https://blog.callstack.io/write-react-native-apps-in-2017-style-with-mobx-e2dffc209fcb
[3] https://github.com/react-navigation/react-navigation/blob/8e8d3d562c9e80616f145f97ffb02dcf2048e67e/docs/guides/Mobx-Integration.md https://github.com/react-navigation/react-navigation/blob/8e8d3d562c9e80616f145f97ffb02dcf2048e67e/docs/guides/Mobx-Integration.md
[4] MobX + React Native:注入存储的方式 https://stackoverflow.com/questions/45670065/mobx-react-native-way-to-inject-stores
[5] MobX - 当我可以在将数据注入 React 组件时使用“inject”时,为什么我应该使用“observer” https://stackoverflow.com/questions/48437102/mobx-why-should-i-use-observer-when-i-could-use-inject-when-injecting-data
[6] 在 React 组件中注入 Store 会导致错误 https://stackoverflow.com/questions/46139278/injecting-store-in-react-component-results-in-error

但我仍然遇到这个错误:

undefined is not a function (evaluating 'decorator(target, property, desc)')

这是我的 App.js 渲染:

render() {
    const hydrate = create({
        storage: AsyncStorage
    });

    hydrate('playerStore', stores.PlayerStore);
    hydrate('settingStore', stores.SettingStore);
    // .then(
    //     // () => console.warn('some hydrated')
    // );

    return <Provider {...stores} >
        <AppWithNavigationState />
    </Provider>;
}

这是我的routeStore.js:

import {observable} from "mobx";
import {action} from "mobx-react/native";
import AppDrawer from "../appDrawer";
import {autobind} from 'core-decorators';

export default class RouteStore {
    @observable.ref navigationState = {
        index: 0,
        routes: [
            { key: "R1", routeName: "ContentStack" },
        ],
    };

    @action
    dispatchNavigation(action, stackState = true) {
        const previousNavState = stackState ? this.navigationState : null;
        return this.navigationState = AppDrawer.router.getStateForAction(action, previousNavState);
    }
}

这是我的 appWithNavigationState.js:

import React from 'react';
import {observer, inject} from "mobx-react/native";
import {addNavigationHelpers} from "react-navigation";
import AppDrawer from "../appDrawer";

@inject(stores => ({ routeStore: stores.RouteStore }))
@observer
export default class AppWithNavigationState extends React.Component {
    render() {
        return (
            <AppDrawer navigation={addNavigationHelpers({
                dispatch: this.props.routeStore.dispatchNavigation,
                state: this.props.routeStore.navigationState,
            })} />
        );
    }
}

我还使用装饰器包,如下所示:

npm install babel-plugin-transform-decorators-legacy --save-dev

并且这个设置在babelrc file:

{
  "presets": ["react-native"],
  "plugins": ["transform-decorators-legacy"]
}

你能帮我解决这个问题吗?


默认情况下,React Native Babel 转换配置不包括对 ES7 装饰器的支持。

您可以通过首先安装装饰器包来添加它们:

npm install babel-plugin-transform-decorators-legacy --save-dev

然后将插件添加到您的.babelrc项目根目录下的文件:

{
  "presets": ["react-native"],
  "plugins": ["transform-decorators-legacy"]
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

undefined 不是一个函数(评估 'decorator(target, property, desc)') 的相关文章

随机推荐