Skip to content

Instantly share code, notes, and snippets.

@clemos
Last active August 27, 2018 15:06
Show Gist options
  • Save clemos/c96bdfeaedd0f8faa4d93da005386bfc to your computer and use it in GitHub Desktop.
Save clemos/c96bdfeaedd0f8faa4d93da005386bfc to your computer and use it in GitHub Desktop.
simple parent/children management
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