React Base Fiddle (JSX)
Starting point for creating JSFiddles with React.
HTML
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="container">
<!-- This element's contents will be replaced with your component. -->
</div>
Babel + JSX
class Hello extends React.Component {
constructor(props){
super(props);
this.state = {
activeID : 1,
}
this.changeActiveID = this.changeActiveID.bind(this);
}
changeActiveID(id){
this.setState({
activeID:id
})
}
render() {
var arr = [{id:1},{id:2},{id:3},{id:4}];
var activeID = this.state.activeID;
return (
<div>
{
arr.map((item,index)=>{
return(
<HelloChild data={item} key={index} index={index} item = {item} id = {activeID} changeActiveID = {this.changeActiveID} />
);
})
}
</div>
);
}
}
class HelloChild extends React.Component {
constructor(props){
super(props);
this.state = {
showExpand : false,
}
this.showExpand = this.showExpand.bind(this);
}
showExpand(id){
var activeID = id;
let showExpand = this.state.showExpand;
this.setState({
showExpand:!showExpand,
},()=>{
this.props.changeActiveID(activeID)
})
}
render() {
var showExpand = this.state.showExpand;
var mainID = this.props.id;
var item = this.props.item;
return (
<div style={{border:'1px solid #ccc',padding:'20px',width:'400px',cursor:'pointer'}} onClick={()=>this.showExpand(item.id)}>
Click Here {this.props.index}
{
showExpand && item.id == mainID ?
<div>
data
<br />
data
<br />data
<br />data
<br />data
<br />data
<br />data
<br />data
<br />data
<br />data
<br />data
<br />data
<br />data
<br />data
<br />data
<br />data
<br />data
...