最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Loading a file as string with React Native packager - Stack Overflow

programmeradmin8浏览0评论

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?

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?

Share Improve this question asked Jun 6, 2016 at 11:56 miklosmemiklosme 7911 gold badge5 silver badges19 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

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

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

Possible solution:

https://github./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.

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.

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.

发布评论

评论列表(0)

  1. 暂无评论