Skip to content

Instantly share code, notes, and snippets.

@chriscauley
Created June 22, 2020 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriscauley/3020da8bef019effde5185ad950734e9 to your computer and use it in GitHub Desktop.
Save chriscauley/3020da8bef019effde5185ad950734e9 to your computer and use it in GitHub Desktop.
class HexBoard extends React.Component {
state = {}
onMouseDown = cell => () => {
this.props.select && this.props.select(cell)
this.setState({selected: cell})
}
onMouseOver = cell => () => {
if (this.props.hover && this.state.hover !== cell) {
this.props.hover(cell)
this.setState({hover: cell})
}
}
onMouseUp = cell => () => {
this.props.move && this.props.move(this.state.selected, cell)
this.setState({selected: null })
}
render() {
const { cells, CellComponent } = this.props
return (
<div>
{board.cells.map(cell => (
<div
className="some_grid_defining_class_here"
onMouseDown={this.onMouseDown(cell)}
onMouseDown={this.onMouseUp(cell)}
onMouseOver={this.onMouseOver(cell)}
>
<CellComponent {...cell} key={cell.id} />
</div>
))}
</div>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment