Last active
December 1, 2019 17:05
-
-
Save rmela/23a3b2796150aa60c0e3cc4ae3f70def to your computer and use it in GitHub Desktop.
Dynamic table creation from React
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
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