Created
August 21, 2019 19:11
-
-
Save TorbenKoehn/dec6a990950649b20c3b75017dc4f13a to your computer and use it in GitHub Desktop.
react-ui.ts
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
<div data-component="HelloWorld" data-props='{"what": "World!"}'></div> |
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 { createElement } from 'react'; | |
import { render } from 'react-dom'; | |
function HelloWorld({ what }) { | |
return <h1>Hello {what}!</h1>; | |
} | |
const components = { | |
HelloWorld, | |
}; | |
function initializeComponents() { | |
const elements = document.querySelectorAll('[data-component]'); | |
for (const el of elements) { | |
const Component = components[el.dataset.component]; | |
const props = el.dataset.props ? JSON.parse(el.dataset.props) : {}; | |
render(createElement(Component, props), el); | |
} | |
} | |
addEventListener('DOMContentLoaded', initializeComponents); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment