After attempting to populate the state using data fetched by RNFS.readDir, I encountered an issue where the state was being reinitialized.
interface fileUnit {
type: string,
index: number,
title: string,
path: string,
date: string,
size: number,
thumbnail: string
}
...
export const Documents = () => {
const hookState = React.useState([])
const documents: fileUnit[] = hookState[0]
const setDocuments: any =hookState[1]
React.useEffect(() => {
RNFS.readDir(RNFS.DocumentDirectoryPath)
.then(async (results: any[]) => {
results.map((file, key) => {
const fileUnit: fileUnit= { type: file.isFile()? 'file': 'folder', index: key, title: file.name, path: file.path, date: file.mtime, size: file.size, thumbnail: ''}
setDocuments([...documents, fileUnit])
})
})
}, [])
}
My expectation was to append new data to the existing state array, but it was instead initialized anew.
Therefore, I wondered if setting the state type as:
const [documents, setDocuments] = React.useState([]);
would solve this issue?