Reactjs code for rendering nested json
import React from 'react';
import logo from './logo.svg';
import './App.css';
const items = [
{ name: 'isa mustofa', email: 'dd@gmail.com' },
{
'address': [
{
street: '123 main st',
city: 'Mountain View',
state: 'California'
},
{
street: '123 main st',
city: 'Irvine',
state: 'California'
}
]
}
];
const addr = (items) => {
let str = [];
if (items) {
items.map((i, index) => {
let p = (index === 0) ? 'Primary:' : 'Secondary:'
str.push(<div key={p}>{p}</div>);
str.push(<div key={"street"+index}>{i.street}</div>);
str.push(<div key={"city"+index}>{i.city}</div>);
str.push(<div key={"stste"+index}>{i.state}</div>);
}
);
}
return (str);
}
const Contact = param =>
<div>
<div>{param.name}</div>
<div>{param.email}</div>
<div>{addr(param.address)}</div>
</div>;
class App extends React.Component {
render() {
return (
<div id='layout'>
{
items.map((item, index) =>
<Contact key={index} name={item.name} address={item.address}>
</Contact>
)
}
</div>
);
}
}
export default App;
Comments
Post a Comment