miklosme
miklosme

Reputation: 791

Loading a file as string with React Native packager

Is it possible with React Native Packager to load a text file as a string? As the same as Webpack's Raw Loader.

Example:

const someString = require('./something.txt');
console.log(someString);

What is the closest way to achieve this?

Upvotes: 12

Views: 13277

Answers (4)

Scott Storch
Scott Storch

Reputation: 803

There is a library for exactly this problem: React-Native-Local-Resource. The library allows you to asynchronously load any type of text file in the way that you're describing.

Upvotes: 3

miklosme
miklosme

Reputation: 791

Possible solution:

https://github.com/callstack-io/haul

Haul is a drop-in replacement for react-native CLI built on open tools like Webpack. It can act as a development server or bundle your React Native app for production.

Upvotes: 3

danvk
danvk

Reputation: 16955

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

While you can't require a text file with React Native, you can require a JSON file. If it's possible to switch to JSON, you'll be set.

Upvotes: 1

Tom Walters
Tom Walters

Reputation: 15616

You'll need to use the React Native FS module which gives you FS-like commands within your React code:

var RNFS = require('react-native-fs')
RNFS.readFile('./something.txt', 'utf8')
    .then((contents) => {
      console.warn(contents)
    })

Upvotes: 5

Related Questions