preactjs/preact-compat là dự án JavaScript với 943 sao trong nhóm Frontend. ATTENTION: The React compatibility layer for Preact has moved to the main preact repo.
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
ATTENTION: preact-compat has moved to the main repo.
The code here is only meant for the older Preact 8.x release line. If you're still on Preact 8.x we highly recommend upgrading to the 10.x release line as it includes significant improvements and a much more stable React compatibility layer.
🚨 Note: This module is for Preact 8.x and prior - Preact X includes compat by default.
For Preact X, please uninstall preact-compat and replace your aliases with preact/compat.
This module is a compatibility layer that makes React-based modules work with Preact, without any code changes.
It provides the same exports as react and react-dom, meaning you can use your build tool of choice to drop it in where React is being depended on.
Interested? Here's an example project that uses preact-compat to work with an existing React library unmodified,
achieving more than 95% reduction in size:
preact-compat-example
Why?
... or really, "why preact"?
React is a great library and a great concept, and has a large community of module authors creating high-quality components.
However, these components are tightly coupled to React through the use of generic package imports (example).
Preact is a tiny (3kb) implementation of the core value of React, and maintains a nearly identical API.
With a shim like this in place, it is possible to use other React-like libraries like Preact, without forking modules just to change their imports.
There are better long-term ways to solve the coupling issue, like using factory functions that accept named generic methods (not just React DI),
as suggested by Eric Elliot. However, since the React community has already authored so many modules in a more explicitly coupled manner, it's worth
having a simple short-term solution for those who would like to liberate themselves from library lock-in.
Installation
You need to install preact-compat first through npm:
npm i --save preact-compat
NOTE: You need to have preact already installed, if you don't, install it like so:
npm i --save preact
Usage with Webpack
Using preact-compat with Webpack is easy.
All you have to do is add an alias for react and react-dom:
{
// ...
resolve: {
alias: {
'react': 'preact-compat',
'react-dom': 'preact-compat',
// Not necessary unless you consume a module using `createClass`
'create-react-class': 'preact-compat/lib/create-react-class',
// Not necessary unless you consume a module requiring `react-dom-factories`
'react-dom-factories': 'preact-compat/lib/react-dom-factories'
}
}
// ...
}
Usage with Browserify
Using preact-compat with Browserify is as simple as installing and configuring aliasify.
... then in your package.json, configure aliasify to alias react and react-dom:
{
// ...
"aliasify": {
"aliases": {
"react": "preact-compat",
"react-dom": "preact-compat",
// Not necessary unless you consume a module using `createClass`
"create-react-class": "preact-compat/lib/create-react-class",
// Not necessary unless you consume a module requiring `react-dom-factories`
"react-dom-factories": "preact-compat/lib/react-dom-factories"
}
}
// ...
}
If you want to use a package that has a peer dependency of React and want it to point to preact-compat you’ll need to set Aliasify to be a global transform. This is not achievable by editing package.json, you’ll need to use the Browserify api and include the global option there:
With the above Webpack or Browserify aliases in place, existing React modules should work nicely:
import React, { Component } from 'react';
import { render } from 'react-dom';
class Foo extends Component {
propTypes = {
a: React.PropTypes.string.isRequired
};
render() {
let { a, b, children } = this.props;
return <div {...{a,b}}>{ children }</div>;
}
}
render((
<Foo a="a">test</Foo>
), document.body);
Use Without Webpack/Browserify
preact-compat and its single dependency prop-types are both published as UMD modules as of preact-compat version 0.6. This means you can use them via a <script> tag without issue:
You can see the above in action with this JSFiddle Example.
PropTypes
preact-compat adds support for validating PropTypes out of the box. This can be disabled the same way it is when using React, by defining a global process.env.NODE_ENV='production'. PropType errors should work the same as in React - the prop-types module used here is published by the React team to replace PropTypes in React.
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/preactjs/preact-compat là nguồn chính thức.
preactjs/preact-compat có bao nhiêu sao?
preactjs/preact-compat có 943 sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/preactjs/preact-compat. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
preactjs/preact-compat có những chủ đề gì?
GitHub topics của preactjs/preact-compat: "compatibility", "preact", "react", "react-dom", "virtual-dom". TopGit xếp repo vào nhóm Frontend.
preactjs/preact-compat có trang demo không?
Dự án có trang chủ ở http://npm.im/preact-compat. Tab "Readme" ở trang này thường có ảnh chụp và hướng dẫn bắt đầu nhanh.
preactjs/preact-compat còn đang phát triển không?
Commit gần nhất trên preactjs/preact-compat là 4.8 năm trước (theo timestamp GitHub). Repo có 124 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
preactjs/preact-compat dùng license gì?
preactjs/preact-compat phát hành theo license MIT. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
preactjs/preact-compat viết bằng ngôn ngữ gì?
preactjs/preact-compat chủ yếu viết bằng JavaScript. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Đọc đầy đủ README ở tab phía trên.
Vẫn đang phân vân về preact-compat?
Một cú bấm sẽ gửi câu hỏi kèm trang này cho AI — xem AI nói gì về preact-compat.