Require json file dynamically in react-native (from thousands of files)












0















I googled so far and tried to find out the solution but not yet.



I know require() works only with static path, so I want alternative ways to solve my problem. I found this answer here but it doesnt make sense for thousands of resources.



Please advise me the best approach to handle such case.



Background



I have thousand of json files that containing app data, and declared all the file path dynamically like below:



export var SRC_PATH = {
bible_version_inv: {
"kjv-ot": "data/bibles/Bible_KJV_OT_%s.txt",
"kjv-nt": "data/bibles/Bible_KJV_NT_%s.txt",
"lct-ot": "data/bibles/Bible_LCT_OT_%s.txt",
"lct-nt": "data/bibles/Bible_LCT_NT_%s.txt",
"leb": "data/bibles/leb_%s.txt",
"net": "data/bibles/net_%s.txt",
"bhs": "data/bibles/bhs_%s.txt",
"n1904": "data/bibles/na_%s.txt",
.....
"esv": "data/bibles/esv_%s.txt",
.....
},
....


As you can see, file path contains '%s' and that should be replace with right string depends on what the user selected.



For example if user select the bible (abbreviation: "kjv-ot") and the chapter 1 then the file named "data/bibles/Bible_KJV_OT_01.txt" should be imported.



I'm not good enough in react-native, just wondering if there is other alternative way to handle those thousands of resource files and require only one at a time by dynamically following the user's selection.



Any suggestions please.










share|improve this question

























  • Can you provide an example scenario? Like what condition would result in what particular result?

    – Uzair A.
    Nov 17 '18 at 4:40











  • @UzairA., edited the question again, condition means the user behavior, so for example user select the "kjv-ot" bible and the chapter 1, then the file name should be "data/bibles/Bible_KJV_OT_01.txt". That means the app contents should be changed following the user's behavior. thanks

    – Tiffgray34
    Nov 19 '18 at 4:25
















0















I googled so far and tried to find out the solution but not yet.



I know require() works only with static path, so I want alternative ways to solve my problem. I found this answer here but it doesnt make sense for thousands of resources.



Please advise me the best approach to handle such case.



Background



I have thousand of json files that containing app data, and declared all the file path dynamically like below:



export var SRC_PATH = {
bible_version_inv: {
"kjv-ot": "data/bibles/Bible_KJV_OT_%s.txt",
"kjv-nt": "data/bibles/Bible_KJV_NT_%s.txt",
"lct-ot": "data/bibles/Bible_LCT_OT_%s.txt",
"lct-nt": "data/bibles/Bible_LCT_NT_%s.txt",
"leb": "data/bibles/leb_%s.txt",
"net": "data/bibles/net_%s.txt",
"bhs": "data/bibles/bhs_%s.txt",
"n1904": "data/bibles/na_%s.txt",
.....
"esv": "data/bibles/esv_%s.txt",
.....
},
....


As you can see, file path contains '%s' and that should be replace with right string depends on what the user selected.



For example if user select the bible (abbreviation: "kjv-ot") and the chapter 1 then the file named "data/bibles/Bible_KJV_OT_01.txt" should be imported.



I'm not good enough in react-native, just wondering if there is other alternative way to handle those thousands of resource files and require only one at a time by dynamically following the user's selection.



Any suggestions please.










share|improve this question

























  • Can you provide an example scenario? Like what condition would result in what particular result?

    – Uzair A.
    Nov 17 '18 at 4:40











  • @UzairA., edited the question again, condition means the user behavior, so for example user select the "kjv-ot" bible and the chapter 1, then the file name should be "data/bibles/Bible_KJV_OT_01.txt". That means the app contents should be changed following the user's behavior. thanks

    – Tiffgray34
    Nov 19 '18 at 4:25














0












0








0








I googled so far and tried to find out the solution but not yet.



I know require() works only with static path, so I want alternative ways to solve my problem. I found this answer here but it doesnt make sense for thousands of resources.



Please advise me the best approach to handle such case.



Background



I have thousand of json files that containing app data, and declared all the file path dynamically like below:



export var SRC_PATH = {
bible_version_inv: {
"kjv-ot": "data/bibles/Bible_KJV_OT_%s.txt",
"kjv-nt": "data/bibles/Bible_KJV_NT_%s.txt",
"lct-ot": "data/bibles/Bible_LCT_OT_%s.txt",
"lct-nt": "data/bibles/Bible_LCT_NT_%s.txt",
"leb": "data/bibles/leb_%s.txt",
"net": "data/bibles/net_%s.txt",
"bhs": "data/bibles/bhs_%s.txt",
"n1904": "data/bibles/na_%s.txt",
.....
"esv": "data/bibles/esv_%s.txt",
.....
},
....


As you can see, file path contains '%s' and that should be replace with right string depends on what the user selected.



For example if user select the bible (abbreviation: "kjv-ot") and the chapter 1 then the file named "data/bibles/Bible_KJV_OT_01.txt" should be imported.



I'm not good enough in react-native, just wondering if there is other alternative way to handle those thousands of resource files and require only one at a time by dynamically following the user's selection.



Any suggestions please.










share|improve this question
















I googled so far and tried to find out the solution but not yet.



I know require() works only with static path, so I want alternative ways to solve my problem. I found this answer here but it doesnt make sense for thousands of resources.



Please advise me the best approach to handle such case.



Background



I have thousand of json files that containing app data, and declared all the file path dynamically like below:



export var SRC_PATH = {
bible_version_inv: {
"kjv-ot": "data/bibles/Bible_KJV_OT_%s.txt",
"kjv-nt": "data/bibles/Bible_KJV_NT_%s.txt",
"lct-ot": "data/bibles/Bible_LCT_OT_%s.txt",
"lct-nt": "data/bibles/Bible_LCT_NT_%s.txt",
"leb": "data/bibles/leb_%s.txt",
"net": "data/bibles/net_%s.txt",
"bhs": "data/bibles/bhs_%s.txt",
"n1904": "data/bibles/na_%s.txt",
.....
"esv": "data/bibles/esv_%s.txt",
.....
},
....


As you can see, file path contains '%s' and that should be replace with right string depends on what the user selected.



For example if user select the bible (abbreviation: "kjv-ot") and the chapter 1 then the file named "data/bibles/Bible_KJV_OT_01.txt" should be imported.



I'm not good enough in react-native, just wondering if there is other alternative way to handle those thousands of resource files and require only one at a time by dynamically following the user's selection.



Any suggestions please.







ios react-native






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 16:10







Tiffgray34

















asked Nov 17 '18 at 3:32









Tiffgray34Tiffgray34

276




276













  • Can you provide an example scenario? Like what condition would result in what particular result?

    – Uzair A.
    Nov 17 '18 at 4:40











  • @UzairA., edited the question again, condition means the user behavior, so for example user select the "kjv-ot" bible and the chapter 1, then the file name should be "data/bibles/Bible_KJV_OT_01.txt". That means the app contents should be changed following the user's behavior. thanks

    – Tiffgray34
    Nov 19 '18 at 4:25



















  • Can you provide an example scenario? Like what condition would result in what particular result?

    – Uzair A.
    Nov 17 '18 at 4:40











  • @UzairA., edited the question again, condition means the user behavior, so for example user select the "kjv-ot" bible and the chapter 1, then the file name should be "data/bibles/Bible_KJV_OT_01.txt". That means the app contents should be changed following the user's behavior. thanks

    – Tiffgray34
    Nov 19 '18 at 4:25

















Can you provide an example scenario? Like what condition would result in what particular result?

– Uzair A.
Nov 17 '18 at 4:40





Can you provide an example scenario? Like what condition would result in what particular result?

– Uzair A.
Nov 17 '18 at 4:40













@UzairA., edited the question again, condition means the user behavior, so for example user select the "kjv-ot" bible and the chapter 1, then the file name should be "data/bibles/Bible_KJV_OT_01.txt". That means the app contents should be changed following the user's behavior. thanks

– Tiffgray34
Nov 19 '18 at 4:25





@UzairA., edited the question again, condition means the user behavior, so for example user select the "kjv-ot" bible and the chapter 1, then the file name should be "data/bibles/Bible_KJV_OT_01.txt". That means the app contents should be changed following the user's behavior. thanks

– Tiffgray34
Nov 19 '18 at 4:25












1 Answer
1






active

oldest

votes


















1














Instead of exporting a flat file, you could export a function that took a parameter which would help build out the paths like this:



// fileInclude.js
export const generateSourcePath = (sub) => {
return {
bible_version_inv: {
"kjv-ot": `data/bibles/Bible_KJV_OT_${sub}.txt`
}
}
}


//usingFile.js
const generation = require('./fileInclude.js');
const myFile = generation.generateSourcePath('mySub');

const requiredFile = require(myFile);


then you would import (or require) this item into your project, execute generateSourcePath('mysub') to get all your paths.






share|improve this answer


























  • as you know require() and import() are working with the static file path not with dynamic variables, if this is the solution then how I use this func with the require().

    – Tiffgray34
    Nov 19 '18 at 4:29













  • You would import this function, and inline require the file that you're attempting to use locally. Require can happen dynamically.

    – Petrogad
    Nov 19 '18 at 14:40











  • any sample script please. so something like this? require (generateSourcePath(sub));

    – Tiffgray34
    Nov 19 '18 at 15:59











  • Updated answer to see how it could be used.

    – Petrogad
    Nov 19 '18 at 19:20











  • Thanks for your help, @Petrogad. By the way still getting error. => invalid call require(myFile)(null) and when I output the myFile to the console simulator says "No bundle URL present".

    – Tiffgray34
    Nov 20 '18 at 3:52











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347927%2frequire-json-file-dynamically-in-react-native-from-thousands-of-files%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Instead of exporting a flat file, you could export a function that took a parameter which would help build out the paths like this:



// fileInclude.js
export const generateSourcePath = (sub) => {
return {
bible_version_inv: {
"kjv-ot": `data/bibles/Bible_KJV_OT_${sub}.txt`
}
}
}


//usingFile.js
const generation = require('./fileInclude.js');
const myFile = generation.generateSourcePath('mySub');

const requiredFile = require(myFile);


then you would import (or require) this item into your project, execute generateSourcePath('mysub') to get all your paths.






share|improve this answer


























  • as you know require() and import() are working with the static file path not with dynamic variables, if this is the solution then how I use this func with the require().

    – Tiffgray34
    Nov 19 '18 at 4:29













  • You would import this function, and inline require the file that you're attempting to use locally. Require can happen dynamically.

    – Petrogad
    Nov 19 '18 at 14:40











  • any sample script please. so something like this? require (generateSourcePath(sub));

    – Tiffgray34
    Nov 19 '18 at 15:59











  • Updated answer to see how it could be used.

    – Petrogad
    Nov 19 '18 at 19:20











  • Thanks for your help, @Petrogad. By the way still getting error. => invalid call require(myFile)(null) and when I output the myFile to the console simulator says "No bundle URL present".

    – Tiffgray34
    Nov 20 '18 at 3:52
















1














Instead of exporting a flat file, you could export a function that took a parameter which would help build out the paths like this:



// fileInclude.js
export const generateSourcePath = (sub) => {
return {
bible_version_inv: {
"kjv-ot": `data/bibles/Bible_KJV_OT_${sub}.txt`
}
}
}


//usingFile.js
const generation = require('./fileInclude.js');
const myFile = generation.generateSourcePath('mySub');

const requiredFile = require(myFile);


then you would import (or require) this item into your project, execute generateSourcePath('mysub') to get all your paths.






share|improve this answer


























  • as you know require() and import() are working with the static file path not with dynamic variables, if this is the solution then how I use this func with the require().

    – Tiffgray34
    Nov 19 '18 at 4:29













  • You would import this function, and inline require the file that you're attempting to use locally. Require can happen dynamically.

    – Petrogad
    Nov 19 '18 at 14:40











  • any sample script please. so something like this? require (generateSourcePath(sub));

    – Tiffgray34
    Nov 19 '18 at 15:59











  • Updated answer to see how it could be used.

    – Petrogad
    Nov 19 '18 at 19:20











  • Thanks for your help, @Petrogad. By the way still getting error. => invalid call require(myFile)(null) and when I output the myFile to the console simulator says "No bundle URL present".

    – Tiffgray34
    Nov 20 '18 at 3:52














1












1








1







Instead of exporting a flat file, you could export a function that took a parameter which would help build out the paths like this:



// fileInclude.js
export const generateSourcePath = (sub) => {
return {
bible_version_inv: {
"kjv-ot": `data/bibles/Bible_KJV_OT_${sub}.txt`
}
}
}


//usingFile.js
const generation = require('./fileInclude.js');
const myFile = generation.generateSourcePath('mySub');

const requiredFile = require(myFile);


then you would import (or require) this item into your project, execute generateSourcePath('mysub') to get all your paths.






share|improve this answer















Instead of exporting a flat file, you could export a function that took a parameter which would help build out the paths like this:



// fileInclude.js
export const generateSourcePath = (sub) => {
return {
bible_version_inv: {
"kjv-ot": `data/bibles/Bible_KJV_OT_${sub}.txt`
}
}
}


//usingFile.js
const generation = require('./fileInclude.js');
const myFile = generation.generateSourcePath('mySub');

const requiredFile = require(myFile);


then you would import (or require) this item into your project, execute generateSourcePath('mysub') to get all your paths.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 19:20

























answered Nov 17 '18 at 19:21









PetrogadPetrogad

3,27452672




3,27452672













  • as you know require() and import() are working with the static file path not with dynamic variables, if this is the solution then how I use this func with the require().

    – Tiffgray34
    Nov 19 '18 at 4:29













  • You would import this function, and inline require the file that you're attempting to use locally. Require can happen dynamically.

    – Petrogad
    Nov 19 '18 at 14:40











  • any sample script please. so something like this? require (generateSourcePath(sub));

    – Tiffgray34
    Nov 19 '18 at 15:59











  • Updated answer to see how it could be used.

    – Petrogad
    Nov 19 '18 at 19:20











  • Thanks for your help, @Petrogad. By the way still getting error. => invalid call require(myFile)(null) and when I output the myFile to the console simulator says "No bundle URL present".

    – Tiffgray34
    Nov 20 '18 at 3:52



















  • as you know require() and import() are working with the static file path not with dynamic variables, if this is the solution then how I use this func with the require().

    – Tiffgray34
    Nov 19 '18 at 4:29













  • You would import this function, and inline require the file that you're attempting to use locally. Require can happen dynamically.

    – Petrogad
    Nov 19 '18 at 14:40











  • any sample script please. so something like this? require (generateSourcePath(sub));

    – Tiffgray34
    Nov 19 '18 at 15:59











  • Updated answer to see how it could be used.

    – Petrogad
    Nov 19 '18 at 19:20











  • Thanks for your help, @Petrogad. By the way still getting error. => invalid call require(myFile)(null) and when I output the myFile to the console simulator says "No bundle URL present".

    – Tiffgray34
    Nov 20 '18 at 3:52

















as you know require() and import() are working with the static file path not with dynamic variables, if this is the solution then how I use this func with the require().

– Tiffgray34
Nov 19 '18 at 4:29







as you know require() and import() are working with the static file path not with dynamic variables, if this is the solution then how I use this func with the require().

– Tiffgray34
Nov 19 '18 at 4:29















You would import this function, and inline require the file that you're attempting to use locally. Require can happen dynamically.

– Petrogad
Nov 19 '18 at 14:40





You would import this function, and inline require the file that you're attempting to use locally. Require can happen dynamically.

– Petrogad
Nov 19 '18 at 14:40













any sample script please. so something like this? require (generateSourcePath(sub));

– Tiffgray34
Nov 19 '18 at 15:59





any sample script please. so something like this? require (generateSourcePath(sub));

– Tiffgray34
Nov 19 '18 at 15:59













Updated answer to see how it could be used.

– Petrogad
Nov 19 '18 at 19:20





Updated answer to see how it could be used.

– Petrogad
Nov 19 '18 at 19:20













Thanks for your help, @Petrogad. By the way still getting error. => invalid call require(myFile)(null) and when I output the myFile to the console simulator says "No bundle URL present".

– Tiffgray34
Nov 20 '18 at 3:52





Thanks for your help, @Petrogad. By the way still getting error. => invalid call require(myFile)(null) and when I output the myFile to the console simulator says "No bundle URL present".

– Tiffgray34
Nov 20 '18 at 3:52


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347927%2frequire-json-file-dynamically-in-react-native-from-thousands-of-files%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?