So I am trying to make my UI in React dependent on my JSON file. I have read a lot of articles in here, but I haven't found something, I could both understand and use. I think loading json data from local file into React JS is the closest I have come, but I am not sure how to apply it to my program.
Pre-knowledge; I am using ES6 through this setup.
I am trying to base my React on a local JSON-file by .map through it and then render a div every time, there's a key. Later I also want to make some of the JSON values to <p></p>
, but I am not that far yet.
I tried to follow the tutorial, which accesses a JSON file via an AJAX call, but couldn't make it work in the above mentioned setup - and yes I found out that I can't use getInitialState in ES6. No matter what I tried, it kept giving me a parse error or can't find myFile.json.
So what I need is something like this
var data = require('./myFile.json');
class theWholeThing extends React.Component {
render() {
<div className="container">
return <makeDiv key={key} item={item}/>;
</div>
}
}
class makeDiv extends React.Component {
{data.levels.map(function (item, key) { //mapping through the key "levels" in json
return <div className="itIsVisible"></div>;
})}
}
So every time there's a a level in "levels", lvl1, lvl2, lvl3, there should be a div. If I make another level in json, there should be another div in the UI.
Any suggestions is appreciated or links to other articles.
I am new to React, json and AJAX calls and little below intermediate (or a bit over beginner) programmer in general.
PS I am not trying to make a game, more of an interactive way of getting information through a simple but intuitive GUI.