PreactReact 的輕量化替代方案
Preact 是 React 的輕量化替代方案,僅有 3KB。并且提供了相同的 ES6 API,還具有組件和 Virtual DOM。
具有以下特性:
-
更接近于實質(zhì):Preact 在 DOM 上實現(xiàn)一個可能是最薄的一層虛擬 DOM 實現(xiàn)。
-
小體積:這將意味可以下載更少的 JavaScript 代碼,解析和執(zhí)行 —— 為您的代碼節(jié)省更多的時間。
-
高性能:不僅是因為他的體積,還因為一個簡單和可預(yù)測的 diff 實現(xiàn),使它成為最快的虛擬 DOM 框架之一。
-
輕量 & 可嵌入:使用 Preact 構(gòu)建應(yīng)用程序的各個部分,而無需復(fù)雜的集成。將 Preact 嵌入到窗口小部件中并應(yīng)用相同的工具或技術(shù),你將可以構(gòu)建一個完整的應(yīng)用程序。
-
生態(tài)系統(tǒng)兼容:可以無縫使用 React 生態(tài)系統(tǒng)中可用的數(shù)千個組件。
示例代碼:
獲取 Github Stars
export default class Stars extends Component {
async componentDidMount() {
let stars = await githubStars(this.props.repo);
this.setState({ stars });
}
render({ repo }, { stars=0 }) {
let url = `//github.com/${repo}`;
return (
<a href={url} class="stars">
?? {stars} Stars
</a>
);
}
}
運行例子
import Stars from './stars'; render( <Stars repo="developit/preact" />, document.body );
評論
圖片
表情
