在使用useRealm()挂钩时,我收到以下错误:
错误:找不到领域上下文.您是否在
<RealmProvider/>?
内调用useRealm()
为什么我会收到这个错误,以及如何纠正它?
我遵循了Reaction Native by领域的快速入门,如下所述:https://www.mongodb.com/docs/realm/sdk/react-native/quick-start/
以下是源代码.
App.jsx:
import TestView from './src/modules/sandbox/TestView';
function App() {
return <TestView />;
}
export default App;
TestView.jsx:
import React from 'react';
import Realm from 'realm';
import { createRealmContext } from '@realm/react';
import TestComponent from './TestComponent';
// Define your object model
class Profile extends Realm.Object {
static schema = {
name: 'Profile',
properties: {
_id: 'objectId',
name: 'string',
},
primaryKey: '_id',
};
}
// Create a configuration object
const realmConfig = {
schema: [Profile],
};
// Create a realm context
const { RealmProvider, useRealm, useObject, useQuery } = createRealmContext(realmConfig);
export default function TestView() {
return (
<RealmProvider>
<TestComponent />
</RealmProvider>
);
}
加入时间:清华2007年01月25日下午3:33
import { StyleSheet, Text, View } from 'react-native';
import React from 'react';
import { useRealm } from '@realm/react';
export default function TestComponent() {
const realm = useRealm();
return (
<View style={styles.container}>
<Text>Aloha</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
backgroundColor: 'white',
},
});
领域和RN版本:
"realm": "^11.10.1", "@realm/react": "^0.5.1", "react-native": "0.71.11",
机器人13:
IOS16.4: