im new to React and I need some help figuring this out!
I have a Json object:
var exampleJson = {
title: "title",
blocks: [
{
type: "block1",
items: [{id: "1", count: 1},
{id: "2", count: 1},]
},
{
type: "block2",
items: [{id: "3", count: 1},
{id: "4", count: 1},]
},
]};
I want to get every block in the object and render the data inside each block(including looping through items inside the block) in a div. The div should have a dynamic onClick-event that registers wich block was clicked.
This is what I got:
var BlockListClass = React.createClass({
blockClicked: function() {
// Here I dont know what block I clicked
},
loopBlocks: function(_data, blockClicked) {
// loop blocks
{_data.map(function(object, i){
var result = <div className="block" key={i}>
{[
<h2 onClick={blockClicked} key={i}> {object.type} </h2>
]}
</div>;
// Loop items
for (var key in object.items) {
if (object.items.hasOwnProperty(key)) {
result.props.children.push(<h2 key={key+10}> {object.items[key].id} </h2>);
}
}
return result;
})}
},
render: function() {
var _data = exampleJson.blocks;
var blockClicked = this.blockClicked;
var renderer = this.loopBlocks(_data,blockClicked);
return(
<div className="BlockList">
{renderer}
</div>
);
}
});
And then render BlockListClass like this:
<BlockListClass />