Skip to content

Instantly share code, notes, and snippets.

@scriptype
Created September 11, 2020 10:18
Show Gist options
  • Save scriptype/8ff3cbc9bf2832be064b250c035068c4 to your computer and use it in GitHub Desktop.
Save scriptype/8ff3cbc9bf2832be064b250c035068c4 to your computer and use it in GitHub Desktop.
Rendering 20 buttons with indexes
const renderComponent = (component, parentNode, method = 'append') => {
if (method === 'append') {
parentNode.appendChild(component.element)
} else {
// To be implemented
}
if (component.props.onClick) {
component.element.addEventListener('click', component.props.onClick)
}
if (component.props.onFocus) {
/// To be implemented
}
}
const Button = (props) => {
const element = document.createElement('button')
element.type = props.type || 'button'
element.textContent = props.text || ''
return {
element,
props
}
}
const buttonCount = 20
;[...new Array(20).keys()].forEach(index => {
const buttonNumber = index + 1
const buttonComponent = Button({
text: buttonNumber,
onClick: () => alert(buttonNumber)
})
renderComponent(buttonComponent, container)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment