【发布时间】:2018-12-01 07:35:34
【问题描述】:
基于此问答:
React wrapper: React does not recognize the `staticContext` prop on a DOM element
答案对我的场景来说不是很好,我有很多道具,真的不喜欢复制粘贴,希望下次接触代码的人都能更新。
所以,我认为可能可行的只是重新利用 React 用来检查属性是否适合在提交之前有条件地删除属性的任何功能。
类似这样的:
import { imaginaryIsDomAttributeFn } from "react"
...
render() {
const tooManyProps = this.props;
const justTheRightProps = {} as any;
Object.keys(tooManyProps).forEach((key) => {
if (imaginaryIsDomAttributeFn(key) === false) { return; }
justTheRightProps[key] = tooManyProps[key];
});
return <div {...justTheRightProps} />
}
我在 Reacts index.t.ts 中找到了 DOMAttributes 和 HTMLAttributes,并且可能将它们变成大量字符串来检查键,但是...我宁愿将其作为最后的手段。
那么,React 是如何进行检查的呢?我可以重复使用他们的代码吗?
【问题讨论】:
-
是的,这非常有效。谢谢。
标签: reactjs