Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load local text file into string variable in ReactNative Project?

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?

like image 221
delete Avatar asked Apr 09 '16 13:04

delete


People also ask

How do I read a local text file in react JS?

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.

How do you use string in react native?

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!


1 Answers

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.

like image 88
Scott Storch Avatar answered Sep 22 '22 20:09

Scott Storch