反应 - 通过 JS 获取支持的属性



我正在构建一个小的实验性应用程序,它应该接受用户的html/svg输入并将其呈现为React虚拟DOM元素。基本上我有:

const dom = new DOMParser().parseFromString(<user input>);
const node = e => React.createElement(
e.localName,
[...e.attributes]
.map(attr => attr.name)
.reduce((props, name) => {
//i need that test
if (<name is a proper react attribute>) {
props[name] = e.getAttribute(name);
}
return props;
}, {}),
[...e.children].map(child => node(child))
);

React 抱怨Unknown props,比如xlink:href输入是否是 svg。所以我需要过滤掉所有"未知道具"。为了做到这一点,将该列表作为JS对象将非常有帮助。

我想知道/想要的是这样的:

import { KnownProps } from 'react';
const testIfProperName = name => 
KnownProps.hasOwnProperty(name);

编写所需的测试。

是否存在这样的列表,如果存在,我可以将其导入到我的程序中吗?

看看 react-html-attributes。

最新更新