React-Native MobX-State-Tree store unavailable on registerComponent

r8xiu3jd  于 2023-04-12  发布在  React
关注(0)|答案(1)|浏览(116)

给定以下设置:

import { AppRegistry } from "react-native";
import { Provider } from "mobx-react";
import Application from "./src/components/Application";
import ApplicationStore from "./src/stores/ApplicationStore";
import { name as appName } from "./app.json";

let app = ApplicationStore.create();

AppRegistry.registerComponent(name, () => (
  <Provider app={app}>
    <Application />
  </Provider>
));

ApplicationStore未定义,因此无法创建...
Cannot read property 'create' of undefined
Module AppRegistry is not a registered callable module (calling runApplication)
有没有更好的方法来设置ApplicationStore,并通过注入使其可用于组件?

const Application = inject("app")(
  observer(
    class Application extends Component {
    ...

这就是我在react apps()中所做的,尽管是通过ReactDOM.render()

rxztt3cl

rxztt3cl1#

好吧,尽职调查是为了赢!

import React from "react";
import { AppRegistry } from "react-native";
import { Provider } from "mobx-react";
import Application from "./src/components/Application";
import { ApplicationStore } from "./src/stores/ApplicationStore";
import { name as appName } from "./app.json";

const app = ApplicationStore.create();
const ApplicationProvider = () => {
  return (
    <Provider app={app}>
      <Application />
    </Provider>
  );
};

AppRegistry.registerComponent(appName, () => ApplicationProvider);

然后在组件内部:

const Application = inject("app")(
  observer(
    class Application extends Component {
      render() {
        return (
          <View style={styles.container}>
            <Text>Application</Text>
          </View>
        );
      }
    }
  )
);

现在,商店可用于所有组件,并在应用程序启动时示例化...

相关问题