In my ReactNative project there are some static text files which i'd like to load into a string variable. Like this way:
var content = loadPlainTextFile("resources/tags.txt");
var tags = content.split("\n");
I tried require in the same way i'm requiring javascript files, but it doesn't work because react native gives exception "Unable to resolve module ./data/tags.txt".
var customData = require('./data/tags.txt');
I guess that require() is not able to handle plain text files?
How to do correctly?
To read a text file in React, we can use the FileReader constructor. to define the showFile function that gets the selected file from e. target.
Use the + operator to concatenate two string variables in React Native: const helloWorld = hello + world; Another version with a hardcoded string: const helloWorld2 = hello + 'World!
The question is specifically asking about how to import a text file using something like:
import textFile from './textFile.txt'
This is possible using React-Native-Local-Resource. The library allows you to asynchronously load any type of text file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With