我注意到,某些库,如classnames,在Preact中随时可用,但其他库,如styled-components,则需要preact-compat。
是什么使得React库在preact中本机不受支持,以至于它需要使用preact-compat?
发布于 2019-02-24 23:12:18
免责声明:我在preact上工作。
react中有几个preact不需要的API。但是,因为已经为这些API开发了现有的第三方库,所以我们发布了preact-compat,它在preact之上重新实现了这些API。
下面是一些例子:
Children-API:
这个接口特别有趣,因为preact根本不需要它。对于preact,children属性始终是一个数组。
// React
Children.forEach(props.children, child => ...);
Children.map(props.children, child => ...);
Children.count(props.children);
// Preact
props.children.forEach(child => ...);
props.children.map(child => ...);
props.children.length;unmountComponentAtNode
这是preact不需要的另一个API,因为我们可以通过呈现null简单地卸载任何树。
import { render } from "preact";
import App from "./app";
// Render App into dom
render(<App />, document.getElementById("root"));
// Unmount tree
render(null, document.getElementById("root"));如果你想删除一个子树而不是根节点,你可以通过从组件返回null来实现。基本上,null始终被视为空值。
findDOMNode
// React
const element = React.findDOMNode(componentInstance);
// In Preact that's just a property
const element = componentInstance.base;在我们的例子中,这甚至适用于函数组件。请注意,在几乎所有情况下,ref都比findDOMNode更受欢迎。
摘要: preact-compat主要包含第三方库的填充程序,希望与react完全兼容。
https://stackoverflow.com/questions/53773807
复制相似问题