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?
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?
Share Improve this question edited Apr 11, 2016 at 16:35 delete asked Apr 9, 2016 at 13:09 deletedelete 19.1k17 gold badges62 silver badges98 bronze badges 3 |2 Answers
Reset to default 17The 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.
Create a constants.js like below, where you are exporting the constants.
module.exports = {
SPRING_CONFIG: {tension: 40, friction: 3},
COLOR: {
COLOR1: '#ffffff',
COLOR2: '#eeeeee',
BLACK_TEXT: '#333',
},
};
Then in whatever file you are trying to import it, import it as...
import CONSTANTS from './constants'; //Make sure you put the right folder path.
console.log(CONSTANTS.COLOR.BLACK_TEXT);
tags.txt
file – Ivan Chernykh Commented Apr 9, 2016 at 13:54export default "some useful text"
but we are loosing the type, and syntax highlighting, which is a shame. Example of some useful extensions:config.yml
query.grapql
shader.glsl
– Joseph Merdrignac Commented Jun 18, 2018 at 10:12