Below is a component that should use user.id
as a partition key from react context, however, when I create a new task it returned an error stating that user/user.id
is undefined.
Please look at the code below:
import React, {FC, SetStateAction, useEffect, useRef, useState} from 'react';import {Text, View} from 'react-native';import TButton from '../../components/button';import Input from '../../components/input';import {useAuth} from '../../lib/realmProvider';import {useRealm} from '../../lib/syncProvider';import {ITarget} from '../../lib/taskInterface';import {Target} from '../../realm/taskSchema';import {ObjectId} from 'bson';const CreateTaskScreen: FC = () => { const [target, setTarge] = useState<Partial<ITarget>>({ _id: '', _partition: user?.id!, description: '', status: false, createAt: new Date(Date.now()).toString(), updateAt: '', }); const {user} = useAuth(); const realm = useRealm(); const targetHandler = () => { try { realm.write(() => { realm.create<Target>('Targets', { _id: new ObjectId(), _partition: target._partition, description: target.description, status: target.status, createAt: target.createAt, updateAt: target.updateAt, }); }); } catch (err) { console.log(err); throw err; } }; const testHandler = () => { console.log(user); }; const descriptionOnChange = (e: {nativeEvent: {text: SetStateAction}}) => { let value = e.nativeEvent.text; setTarge({description: value}); }; useEffect(() => { console.log(`from create target ${user}`); }, [user]); return (<View><Text>Create a new Task</Text><Input placeholder={'Targe Description'} onChange={descriptionOnChange} value={target.description} multiline={true} /><TButton title={'Add Target'} onPress={targetHandler} /><TButton title={'Test Button'} onPress={testHandler} /></View> );};export default CreateTaskScreen;
When the component rendersuseEffect
returns [object Object]
.While testHandler
returns {}
which is empty
I really don't understand what I could be doing wrong with react context