Created
August 27, 2018 08:23
-
-
Save cpq/da2f1b11a8e7ddc61e137eaa9fb5201f to your computer and use it in GitHub Desktop.
Minimal preact app without babel and JSX
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Preact without Babel or JSX</title> | |
</head> | |
<body> | |
<script src="https://unpkg.com/preact"></script> | |
<script> | |
'use strict'; | |
const { Component, h, render } = window.preact; | |
/** Example classful component */ | |
class App extends Component { | |
componentDidMount() { | |
this.setState({ message: 'Hello2!' }); | |
} | |
render({ }, { message }) { | |
return h('div', { id: 'app' }, h(Header, { message }), h(Main)); | |
} | |
} | |
/** Components can just be pure functions */ | |
const Header = ({ message }) => h('header', null, h('h1', null, 'App'), message && h('h2', null, message)); | |
/** Instead of JSX, use: h(type, props, ...children) */ | |
const Main = () => h('main', null, h('ul', null, [1, 2, 3].map(item => h('li', { id: item }, 'Item ' + item)))); | |
render(h(App), document.body); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment