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

标签 javascript reactjs

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

相关文章:

javascript - 从数组中获取一个函数,javascript

reactjs - Jest 和 React Native : How to mock AccessibilityInfo

javascript - 什么是在 JS 模块加载上下文中立即执行的工厂函数?

reactjs - 如何在 React js 中添加样式以响应自动完成

javascript - 如何将图像从 Node.js 发送到 React.js?

reactjs - 如何在react中跳转到具有特定id的div

javascript - 缓慢时如何加快 native react 中的联系人处理速度?

javascript - 使用innerHTML 将值打印到输入标签

javascript - 在 View 中显示 SVG 动画

javascript - 在 IE 中输入字段不显示光标或无法输入字母