Skip to content

Instantly share code, notes, and snippets.

@rmela
Last active December 1, 2019 17:05
Show Gist options
  • Save rmela/23a3b2796150aa60c0e3cc4ae3f70def to your computer and use it in GitHub Desktop.
Save rmela/23a3b2796150aa60c0e3cc4ae3f70def to your computer and use it in GitHub Desktop.
Dynamic table creation from React
import React, { useState } from 'react'
import './App.css'
function Cell( props ) {
const [ state, setState ] = useState( props.alive )
return <td class={ props.alive ? 'alive' : 'dead' }>r{props.row}c{props.col}</td>
}
function Table( props ) {
function *colgen(rownum) {
for( let col = 0; col < props.cols; ++col ) {
yield Cell( { row: rownum, col: col, alive: false } )
}
}
function *rowgen( ) {
for( let row = 0; row < props.rows; ++row ) {
yield <tr>{ Array.from( colgen(row) ) }</tr>
}
}
let rows = Array.from( rowgen() )
return <table><tbody>{rows}</tbody></table>
}
function App(props) {
return Table( { rows:5, cols:4 } )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment