I'm trying to render a list based on an array of objects grouped by status as per const elements on snippet bellow.
I'm aware that I could create smaller components here, but I would really like to make my renderMethod works.
The logic is fine as per this jsbin:
https://jsbin.com/cejiziyupa/edit?js,console
I can't figure out what I'm doing about react and JSX.
Any ideas?
Here is my code on codepen - http://codepen.io/vinicius5581/pen/rjJRvM?editors=0011
Component:
class MyComponent extends React.Component{
constructor(props){
super(props)
renderMethod().bind(this);
}
renderMethod(){
const elements = [
{ "id": 1, "label": "element1", "status": "status1"},
{ "id": 2, "label": "element2","status": "status2"},
{"id": 3, "label": "element3", "status": "status6"},
{ "id": 4, "label": "element3", "status": "status10"}
]
const groups = [
{ "name": "group1", "status" : ["status1", "status2", "status3", "status4"] },
{ "name": "group2", "status" : ["status5", "status6", "status7", "status8"] },
{ "name": "group3", "status" : ["status9", "status10"] }
]
return (
groups.map((group) => {
return(
console.log(group.name);
<h1>{group.name}</h1>
elements.filter((element) => group.status.includes(element.status)).map((element) =>{
return(
console.log(element.id);
<div>
<h1>{element.label}</h1>
<p>{element.status}</p>
</div>
)
})
)
})
)
}
render(){
return(
{this.renderMethod()}
)
}
}
ReactDOM.render(<MyComponent />, document.getElementById('root'));
Html:
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>
return (a; b)in JavaScript. Also your code pen is not transpiling the JSX to React functions, so the browser doesn't understand<div>as JavaScript syntax.