5

I want to read and display the text contents from a .txt file which is saved locally to the app build with react native.

The text is huge hence i cant hard code it to the code.

Joji Thomas
  • 173
  • 1
  • 2
  • 6
  • 1
    Do you have the option of putting it in a `json` file? Then you could import it like you are used to. `const hugeText = require('./my-huge-text.json').text;` – Tholle Jul 04 '18 at 10:59
  • @Tholle No there's no option to put them to json file . Have to work it out with reading them as text file(.txt). – Joji Thomas Jul 04 '18 at 11:37
  • I see, that's unfortunate. I'm not sure it's even possible to import a `txt` file in React Native. – Tholle Jul 04 '18 at 11:49
  • 1
    Use `XMLHttpRequest` to read file: https://stackoverflow.com/questions/14446447/how-to-read-a-local-text-file – Vikram Biwal Jul 04 '18 at 12:32
  • @CrazyVK I dont think that'll work with ReactNative – Joji Thomas Jul 04 '18 at 13:29

1 Answers1

3

I think you are using Expo,

You can use expo filesystem https://docs.expo.io/versions/latest/sdk/filesystem

Which is a native wrapper for platform specific system file management methods. You can read and change files managed in your app's domain.

For Eg:

Expo.FileSystem.readAsStringAsync(fileUri)

Burak Tokak
  • 1,810
  • 1
  • 20
  • 27