【发布时间】:2018-12-14 05:08:38
【问题描述】:
我注意到某些库(例如 classnames)在 Preact 中很容易使用,但其他库(例如 styled-components)需要 preact-compat。
是什么让 React 库在 preact 中不受本机支持而需要使用 preact-compat?
【问题讨论】:
-
为
react构建的库通常具有与react或其依赖库相关的导入语句。为了在不修改的情况下使用preact中的同一个库,preact-compat提供了一个compatibility layer。像classnames这样的库不依赖于 react,因此它们不需要preact-compat。 preact-compat github 自述文件很好地解释了他们如何实现此兼容层。 -
现在
preact-compat(单独的 shim 存储库)不再维护自 PreactX (v10) 及更高版本。它现在是 Preact 主要 repo 的一部分,所以你可以从preact/compat获取你需要的东西(注意中间的斜线):import { PureComponent, memo, forwardRef, createPortal } from 'preact/compat'preactjs.com/guide/v10/switching-to-preact