gpt4 book ai didi

javascript - React——通过JS获取支持的属性

转载 作者:行者123 更新时间:2023-11-30 21:20:32 25 4
gpt4 key购买 nike

我正在构建一个小的实验性应用程序,它应该接受用户 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))
);

如果输入是 svg,React 会提示 Unknown props,比如 xlink:href。所以我需要过滤掉所有那些 »unknown Props«。为了做到这一点,将该列表作为 JS 对象将非常有帮助。

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

import { KnownProps } from 'react';

const testIfProperName = name =>
KnownProps.hasOwnProperty(name);

编写所需的测试。

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

最佳答案

关于javascript - React——通过JS获取支持的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45230337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com