Search code examples
react-nativefilenamesreact-native-fs

How to get files in main directory in a react native project?


I am trying to get file names in main directory of my react native project. I have used "react-native-fs" to do it.

Here is my code;

const getFileNames = () => {
    const path = FS.DocumentDirectoryPath;
    console.log(path);
};

And it doesn't give me App.js, index.js, ... It gives me this result; /data/user/0/com.mrkennedy/files

Versions;

"react-native": "0.62.2",
"react-native-fs": "^2.16.6"

Solution

  • You've just got path, to read the dir's content use readDir: https://github.com/itinance/react-native-fs#readdirdirpath-string-promisereaddiritem which will return you array of objects, each describing file. See this example: https://github.com/itinance/react-native-fs#basic.

    Moreover, you won't access App.js etc. using DocumentDirectoryPath, document directory is not equivalent to your project's root directory. I don't think it's possible to access project's root without special configuration as package server bundles all JavaScript files into single file.