Preact - Petit React
Date: 2019-12-02Last modified: 2022-10-07
Ciclo de vida
componentWillMount
: Antes que o componente seja montado no DOMcomponentDidMount
: Depois que o componente é montado no DOMcomponentWillUnmount
: Antes da remoção do DOMcomponentWillReceiveProps
: Antes que novas props sejam aceitasshouldComponentUpdate
: Antes derender()
. Retorne false caso queiram pular orender()
componentWillUpdate
: Antesrender()
componentDidUpdate
: Depoisrender()
Preact sem babel ou JSX
index.html
<!DOCTYPE html>
<html>
<head>
<title>Preact sem Babel or JSX</title>
</head>
<body>
<script src="https://unpkg.com/preact"></script>
<script src="index.js"></script>
</body>
</html>
index.js
'use strict';
const { Component, h, render } = window.preact;
/** Example classful component */
class App extends Component {
componentDidMount() {
this.setState({ message:'Hello!' });
}
render(props, state) {
return (
h('div', {id:'app'},
h(Header, { message: state.message }),
h(Main)
)
);
}
}
/** Components can just be pure functions */
const Header = (props) => {
return h('header', null,
h('h1', null, 'App'),
props.message && h('h2', null, props.message)
);
};
/** Instead of JSX, use: h(type, props, ...children) */
class Main extends Component {
render() {
const items = [1,2,3,4,5].map( (item) => (
h('li', {id:item}, 'Item '+item)
));
return (
h('main', null,
h('ul', null, items)
)
);
}
}
render(h(App), document.body);
Exemplo de preact com mitt
https://codepen.io/developit/pen/rjMEwW?editors=0010
Referências
- Preact
- https://preactjs.com/guide/getting-started
- https://blog.kiprosh.com/getting-started-with-preactjs-a-step-by-step-guide-f3197f871753/
- https://github.com/developit/preact-without-babel
- https://blog.moove-it.com/getting-started-preact-progressive-web-apps/
- https://blog.theodo.fr/2017/11/preact-progressive-web-app-webpack-material-design-web-apis/
- http://react-materialize.github.io/react-materialize/?path=/story/react-materialize--welcome
- https://project-awesome.org/ooade/awesome-preact