Unable to Import JSON data from local file using ES6 import in a React Redux Webpack App
I am trying to learn building a React + Redux application architecture using Web Pack 3 for bundling. I am trying to work with temporary JSON files for data sets and importing them into Service files which want to use the data returned from the imported JSON.
I try to use the imported JSON in Promise.resolve(JSON) and somehow the value seems to be undefined.
Note: This block is inside in IIFE block.
Find the service code below.
import jsonVal from './josnFile.json';
export default (function() {
return {
method1: methodName1,
method2: methodName2,
...
}
function methodName1(){
return new Promise((resolve, reject) => {
resolve(jsonVal);
// ...usual then, catch blocks for returning rejects
});
}
}());
After all this flow, I have set up the reducers and actions to appropriately update the Redux state. But no update happens because the JSON data is always undefined and when I try to pass this down to the presentational React components via Redux connect (mapStateToProps), its undefined too.
I want to know what should I do to include this JSON for mocking APIs.
Whether I have to use a JSON loader in webpack or any other solution?
Note: I am using babel-node to transpile ES6 code
json reactjs webpack redux
add a comment |
I am trying to learn building a React + Redux application architecture using Web Pack 3 for bundling. I am trying to work with temporary JSON files for data sets and importing them into Service files which want to use the data returned from the imported JSON.
I try to use the imported JSON in Promise.resolve(JSON) and somehow the value seems to be undefined.
Note: This block is inside in IIFE block.
Find the service code below.
import jsonVal from './josnFile.json';
export default (function() {
return {
method1: methodName1,
method2: methodName2,
...
}
function methodName1(){
return new Promise((resolve, reject) => {
resolve(jsonVal);
// ...usual then, catch blocks for returning rejects
});
}
}());
After all this flow, I have set up the reducers and actions to appropriately update the Redux state. But no update happens because the JSON data is always undefined and when I try to pass this down to the presentational React components via Redux connect (mapStateToProps), its undefined too.
I want to know what should I do to include this JSON for mocking APIs.
Whether I have to use a JSON loader in webpack or any other solution?
Note: I am using babel-node to transpile ES6 code
json reactjs webpack redux
1. Are you sure the path to json is correct? I see that you're trying to import./josnFile.json
. There's a typo: "josnFile.json" -> "jsonFile.json" ;) 2. Have you tried with json-loader for webpack? npmjs.com/package/json-loader
– lukaleli
Aug 23 '17 at 21:06
1
on a side note, you don't need an IIFE because everything is scoped to the module
– azium
Aug 23 '17 at 21:50
@jimmyweb - Yes, Path to json is correct, this is just a sample code I used here, and also I have tried using webpack JSON loader. Still same results.
– BadriNarayanan Sridharan
Aug 24 '17 at 21:31
add a comment |
I am trying to learn building a React + Redux application architecture using Web Pack 3 for bundling. I am trying to work with temporary JSON files for data sets and importing them into Service files which want to use the data returned from the imported JSON.
I try to use the imported JSON in Promise.resolve(JSON) and somehow the value seems to be undefined.
Note: This block is inside in IIFE block.
Find the service code below.
import jsonVal from './josnFile.json';
export default (function() {
return {
method1: methodName1,
method2: methodName2,
...
}
function methodName1(){
return new Promise((resolve, reject) => {
resolve(jsonVal);
// ...usual then, catch blocks for returning rejects
});
}
}());
After all this flow, I have set up the reducers and actions to appropriately update the Redux state. But no update happens because the JSON data is always undefined and when I try to pass this down to the presentational React components via Redux connect (mapStateToProps), its undefined too.
I want to know what should I do to include this JSON for mocking APIs.
Whether I have to use a JSON loader in webpack or any other solution?
Note: I am using babel-node to transpile ES6 code
json reactjs webpack redux
I am trying to learn building a React + Redux application architecture using Web Pack 3 for bundling. I am trying to work with temporary JSON files for data sets and importing them into Service files which want to use the data returned from the imported JSON.
I try to use the imported JSON in Promise.resolve(JSON) and somehow the value seems to be undefined.
Note: This block is inside in IIFE block.
Find the service code below.
import jsonVal from './josnFile.json';
export default (function() {
return {
method1: methodName1,
method2: methodName2,
...
}
function methodName1(){
return new Promise((resolve, reject) => {
resolve(jsonVal);
// ...usual then, catch blocks for returning rejects
});
}
}());
After all this flow, I have set up the reducers and actions to appropriately update the Redux state. But no update happens because the JSON data is always undefined and when I try to pass this down to the presentational React components via Redux connect (mapStateToProps), its undefined too.
I want to know what should I do to include this JSON for mocking APIs.
Whether I have to use a JSON loader in webpack or any other solution?
Note: I am using babel-node to transpile ES6 code
json reactjs webpack redux
json reactjs webpack redux
edited Aug 23 '17 at 23:20
asked Aug 23 '17 at 20:52
BadriNarayanan Sridharan
516
516
1. Are you sure the path to json is correct? I see that you're trying to import./josnFile.json
. There's a typo: "josnFile.json" -> "jsonFile.json" ;) 2. Have you tried with json-loader for webpack? npmjs.com/package/json-loader
– lukaleli
Aug 23 '17 at 21:06
1
on a side note, you don't need an IIFE because everything is scoped to the module
– azium
Aug 23 '17 at 21:50
@jimmyweb - Yes, Path to json is correct, this is just a sample code I used here, and also I have tried using webpack JSON loader. Still same results.
– BadriNarayanan Sridharan
Aug 24 '17 at 21:31
add a comment |
1. Are you sure the path to json is correct? I see that you're trying to import./josnFile.json
. There's a typo: "josnFile.json" -> "jsonFile.json" ;) 2. Have you tried with json-loader for webpack? npmjs.com/package/json-loader
– lukaleli
Aug 23 '17 at 21:06
1
on a side note, you don't need an IIFE because everything is scoped to the module
– azium
Aug 23 '17 at 21:50
@jimmyweb - Yes, Path to json is correct, this is just a sample code I used here, and also I have tried using webpack JSON loader. Still same results.
– BadriNarayanan Sridharan
Aug 24 '17 at 21:31
1. Are you sure the path to json is correct? I see that you're trying to import
./josnFile.json
. There's a typo: "josnFile.json" -> "jsonFile.json" ;) 2. Have you tried with json-loader for webpack? npmjs.com/package/json-loader– lukaleli
Aug 23 '17 at 21:06
1. Are you sure the path to json is correct? I see that you're trying to import
./josnFile.json
. There's a typo: "josnFile.json" -> "jsonFile.json" ;) 2. Have you tried with json-loader for webpack? npmjs.com/package/json-loader– lukaleli
Aug 23 '17 at 21:06
1
1
on a side note, you don't need an IIFE because everything is scoped to the module
– azium
Aug 23 '17 at 21:50
on a side note, you don't need an IIFE because everything is scoped to the module
– azium
Aug 23 '17 at 21:50
@jimmyweb - Yes, Path to json is correct, this is just a sample code I used here, and also I have tried using webpack JSON loader. Still same results.
– BadriNarayanan Sridharan
Aug 24 '17 at 21:31
@jimmyweb - Yes, Path to json is correct, this is just a sample code I used here, and also I have tried using webpack JSON loader. Still same results.
– BadriNarayanan Sridharan
Aug 24 '17 at 21:31
add a comment |
active
oldest
votes
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45849081%2funable-to-import-json-data-from-local-file-using-es6-import-in-a-react-redux-web%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45849081%2funable-to-import-json-data-from-local-file-using-es6-import-in-a-react-redux-web%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1. Are you sure the path to json is correct? I see that you're trying to import
./josnFile.json
. There's a typo: "josnFile.json" -> "jsonFile.json" ;) 2. Have you tried with json-loader for webpack? npmjs.com/package/json-loader– lukaleli
Aug 23 '17 at 21:06
1
on a side note, you don't need an IIFE because everything is scoped to the module
– azium
Aug 23 '17 at 21:50
@jimmyweb - Yes, Path to json is correct, this is just a sample code I used here, and also I have tried using webpack JSON loader. Still same results.
– BadriNarayanan Sridharan
Aug 24 '17 at 21:31