【发布时间】:2021-04-30 20:15:50
【问题描述】:
我正在从 Realm 文件数据库中提取数据,但数据似乎只出现在第二次渲染中。这是我的代码:
import React, { useState, useEffect } from 'react'
import {getAllArchive} from '../Schemas/activitiesArchiveSchema';
const Stats = () => {
const [Archives, setArchives] = useState();
useEffect(() => {
(async () => setArchives(await getAllArchive()))();
console.log(JSON.stringify(Archives))
}, []);
数据库功能:
export const getAllArchive = () => {
return new Promise((resolve, reject) => {
Realm.open(databaseOptions).then(realm => {
let activitiesList = realm.objects(ARCHIVE_LIST_SCHEMA)
resolve(activitiesList)
}).catch((error) => reject(error))
})
}
输出:
LOG undefined
LOG {"0":{"title":"sw2","records":{"0":{"date":"4/28/2021","seconds":7,"minutes":0,"hours":1},"1":{"date":"4/27/2021","seconds":7,"minutes":0,"hours":0},"2":{"date":"4/28/2021","seconds":7,"minutes":0,"hours":0}}},"1":{"title":"jj","records":{"0":{"date":"4/25/2021","seconds":0,"minutes":55,"hours":1}}},"2":{"title":"l,kl","records":{"0":{"date":"4/28/2021","seconds":4,"minutes":12,"hours":1}}},"3":{"title":"fsdfdsf","records":{"0":{"date":"4/25/2021","seconds":4,"minutes":43,"hours":1}}},"4":{"title":"sideProj","records":{"0":{"date":"4/26/2021","seconds":30,"minutes":29,"hours":1},"1":{"date":"4/27/2021","seconds":15,"minutes":18,"hours":1}}},"5":{"title":"work","records":{"0":{"date":"4/28/2021","seconds":36,"minutes":13,"hours":0},"1":{"date":"4/28/2021","seconds":37,"minutes":11,"hours":3}}},"6":{"title":"plp","records":{"0":{"date":"4/25/2021","seconds":2,"minutes":12,"hours":3}}},"7":{"title":"fff","records":{"0":{"date":"4/25/2021","seconds":3,"minutes":10,"hours":2}}},"8":{"title":"Work","records":{"0":{"date":"4/27/2021","seconds":1,"minutes":0,"hours":0}}},"9":{"title":"chilling","records":{"0":{"date":"4/27/2021","seconds":40,"minutes":4,"hours":3},"1":{"date":"4/27/2021","seconds":0,"minutes":0,"hours":0}}}}
有人可以指出代码中的任何问题吗?谢谢
【问题讨论】:
标签: javascript reactjs react-native react-hooks realm