“useRealm在Realm和React Native中无法正常工作”

huangapple go评论121阅读模式
英文:

useRealm is not working properly in Realm and React Native

问题

这是我的代码

import React from 'react';
import Realm from 'realm';
import { createRealmContext } from '@realm/react';
import CButton from './src/components/atoms/CButton';

class Profile extends Realm.Object<Profile> {
  _id!: Realm.BSON.ObjectId;
  name!: string;
  static schema = {
    name: 'Profile',
    properties: {
      _id: 'objectId',
      name: 'string',
    },
    primaryKey: '_id',
  };
}

const realmConfig: Realm.Configuration = {
  schema: [Profile],
};

const { RealmProvider, useRealm, useObject, useQuery } =
  createRealmContext(realmConfig);

const RestOfApp = () => {
  const realm = useRealm();
  const changeProfileName = (profileToChange: Profile, newName: string) => {
    realm.write(() => {
      profileToChange.name = newName;
    });
  };

  return <CButton onClick={() => {}}>Test</CButton>;
};

function App(): JSX.Element {
  const realm = useRealm();

  return (
    <RealmProvider fallback={() => null}>
      <RestOfApp />
    </RealmProvider>
  );
}

我在RealmProvider内部使用了useRealm,但仍然在运行应用程序时遇到以下错误。

错误:未找到Realm上下文。您是否在<RealmProvider/>内部调用了useRealm()?

请帮助我解决这个问题,提前感谢。

英文:

Here is my code

import React from &#39;react&#39;;
import Realm from &#39;realm&#39;;
import {createRealmContext} from &#39;@realm/react&#39;;
import CButton from &#39;./src/components/atoms/CButton&#39;;

class Profile extends Realm.Object&lt;Profile&gt; {
  _id!: Realm.BSON.ObjectId;
  name!: string;
  static schema = {
    name: &#39;Profile&#39;,
    properties: {
      _id: &#39;objectId&#39;,
      name: &#39;string&#39;,
    },
    primaryKey: &#39;_id&#39;,
  };
}

const realmConfig: Realm.Configuration = {
  schema: [Profile],
};

const {RealmProvider, useRealm, useObject, useQuery} =
  createRealmContext(realmConfig);

const RestOfApp = () =&gt; {
  const realm = useRealm();
  const changeProfileName = (profileToChange: Profile, newName: string) =&gt; {
    realm.write(() =&gt; {
      profileToChange.name = newName;
    });
  };

  return &lt;CButton onClick={() =&gt; {}}&gt;Test&lt;/CButton&gt;;
};

function App(): JSX.Element {
  const realm = useRealm();

  return (
    &lt;RealmProvider fallback={() =&gt; null}&gt;
      &lt;RestOfApp /&gt;
    &lt;/RealmProvider&gt;
  );
}

I am using useRealm inside RealmProvider but still getting following error while running the app.

> Error: Realm context not found. Did you call useRealm() within a &lt;RealmProvider/&gt;?

Help me on this, how can I fix? Thanks in advance

答案1

得分: 1

你可以直接使用@realm/react获取Realm的钩子和提供程序(Realm版本 >= v0.5.0)。如果这没有帮助,可以查看这个问题:https://github.com/realm/realm-js/issues/5957。

英文:

You can get the realm hooks and Providers using @realm/react directly. (Realm version >= v0.5.0)

import React from &#39;react&#39;;
import Realm from &#39;realm&#39;;
import {RealmProvider, useRealm, useObject, useQuery} from &#39;@realm/react&#39;;
import CButton from &#39;./src/components/atoms/CButton&#39;;

class Profile extends Realm.Object&lt;Profile&gt; {
  _id!: Realm.BSON.ObjectId;
  name!: string;
  static schema = {
    name: &#39;Profile&#39;,
    properties: {
      _id: &#39;objectId&#39;,
      name: &#39;string&#39;,
    },
    primaryKey: &#39;_id&#39;,
  };
}

const realmConfig: Realm.Configuration = {
  schema: [Profile],
};

const RestOfApp = () =&gt; {
  const realm = useRealm();
  const changeProfileName = (profileToChange: Profile, newName: string) =&gt; {
    realm.write(() =&gt; {
      profileToChange.name = newName;
    });
  };

  return &lt;CButton onClick={() =&gt; {}}&gt;Test&lt;/CButton&gt;;
};

function App(): JSX.Element {
  const realm = useRealm();

  return (
    &lt;RealmProvider {...realmConfig} fallback={() =&gt; null}&gt;
      &lt;RestOfApp /&gt;
    &lt;/RealmProvider&gt;
  );
}

If this didn't help then this issue might also be helpful :
https://github.com/realm/realm-js/issues/5957

huangapple
  • 本文由 发表于 2023年6月8日 12:10:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428559.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定