How to check file exist in React Native
Check Full Story…
To check if a file exists in React Native, you can use the RNFS
(React Native File System) library. Here is an example:
First, you need to install react-native-fs
by running this command in your project directory:
npm install react-native-fs
Then, you can use the RNFS.exists()
method to check if a file exists. Here is an example code:
In this example, RNFS.DocumentDirectoryPath
specifies the path to the app's document directory, and example.txt
is the name of the file you want to check. The RNFS.exists()
method returns a promise that resolves to true
if the file exists and false
if it does not exist. If there is an error, the promise is rejected and an error message is logged to the console.
Note that RNFS
library can only access files within the app's sandbox, such as the document directory and cache directory, so you cannot use it to check if a file exists in external storage or other apps' directories.
Here I’ve used ‘react-native-fs’ package to check file exist, you can also check file exist without this package.
Using FileSystem API