John
John

Reputation: 4382

Load a JSON file at runtime

How do I load a local json file in react native project at runtime where the local file will be a variable name?

Example:

This works

var data = require("../../0001.json");

I want this to work:

var path = "../../0001.json";
var data = require(path);

I'm open to alternatives to require.

TLDR: How do I open a file at runtime in react native. Load local data files as required by the app?

Upvotes: 0

Views: 2179

Answers (1)

hazardous
hazardous

Reputation: 10837

If you are using webpack, you could simply use require.ensure. But since all you want to load is JSON data, why don't simply use file API to read the JSON into a stream and then use JSON.parse?

Upvotes: 1

Related Questions