Last active
August 27, 2018 15:06
-
-
Save clemos/c96bdfeaedd0f8faa4d93da005386bfc to your computer and use it in GitHub Desktop.
simple parent/children management
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Parent extends React.Component { | |
state = { | |
currentCell: null, | |
cells: [] | |
}; | |
onOpenCell = (cell) => { | |
this.setState({ | |
currentCell: cell, | |
}); | |
} | |
onCloseCell = () => { | |
this.setState({ | |
currentCell: null, | |
}); | |
} | |
render() { | |
const {currentCell, cells} = this.state; | |
const {onCloseCell, onOpenCell} = this; | |
if( currentCell ) { | |
return <CellDetails | |
cell={currentCell} | |
onCloseCell={onCloseCell} | |
/>; | |
} else { | |
return <CellList | |
cells={cells} | |
onOpenCell={onOpenCell} | |
/>; | |
} | |
} | |
} | |
const CellDetails = ({currentCell, onCloseCell}) => ( | |
<div> | |
<button onClick={onCloseCell}>Go back</button> | |
<h1>{currentCell.title}</h1> | |
<p> ... </p> | |
</div> | |
); | |
const CellList = ({cells = [], onOpenCell}) => ( | |
<ul> | |
{cells.map( cell => ( | |
<li> | |
<a onClick={() => onOpenCell(cell)}>View details</a> | |
</li> | |
)} | |
</ul> | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment