Search code examples
typescriptreact-nativerealm

how to type an array of objects coming from realmDB?


Error: Argument of type 'Results<Courses[] & Object>' is not assignable to parameter of type 'SetStateAction<Courses[]>'. Type 'Results<Courses[] & Object>' does not have the following properties of type 'Courses[]': pop, push, reverse, shift, and more 6.ts(2345)

// my interface
interface Courses {
  id: string;
  name: string;
  image: string;
}

const [courses, setCourses] = useState<Courses[]>([]);

useEffect(() => {
    async function getOfflineCourses() {
      const realmDB = await realm;

      realmDB.write(() => {
        const offlineCourses = realmDB.objects<Courses[]>('Course');
        setCourses(offlineCourses);
      });
    }
    getOfflineCourses();
  }, [realm]);

Solution

  • I got the return I was hoping to add to my state using the following function:

    setCourses(offlineCourses.toJSON());