英文:
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 '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>
);
}
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 <RealmProvider/>
?
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 'react';
import Realm from 'realm';
import {RealmProvider, useRealm, useObject, useQuery} 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 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 {...realmConfig} fallback={() => null}>
<RestOfApp />
</RealmProvider>
);
}
If this didn't help then this issue might also be helpful :
https://github.com/realm/realm-js/issues/5957
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论