javascript - 在组件内使用 React.createElement 的 TypeScript 无法工作/编译

标签 javascript reactjs typescript

在我的 ReactTypeScript 应用程序中,我只需要创建自定义组件的新实例,在我的例子中,该实例会显示通知。

通知的属性是从localStorage读取的。 由于通知需要对高度进行一些调整,因此我需要创建一个新实例(因此调用 componentDidMount)。

但是,当调用我的渲染

const { props: { }, state: { notificationProps } } = this;

const Element = React.createElement(Vtm.Notification, notificationProps, null);

return <Element />;

我收到一堆错误:

JSX element type 'Element' does not have any construct or call signatures.

来自 ReSharper:

_$_$RsRpExt"FromFile:\node_modules\@types\react\index.d.ts, module=JavaScriptReferencedFilesModule:Referenced external files".React.createElement : {
    (type: "input", props?: InputHTMLAttributes<HTMLInputElement> & ClassAttributes<HTMLInputElement>, ...children: ReactNode[]) => DetailedReactHTMLElement<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>; 
    <P extends HTMLAttributes<T extends HTMLElement>, T extends HTMLElement>(type: "a" | "abbr" | "address" | "area" | "article" | "aside" | "audio" | "b" | "base" | "bdi" | "bdo" | "big" | "blockquote" | "body" | "br" | "button" | "canvas" | "caption" | "cite" | "code" | "col" | "colgroup" | "data" | "datalist" | "dd" | "del" | "details" | "dfn" | "dialog" | "div" | "dl" | "dt" | "em" | "embed" | "fieldset" | "figcaption" | "figure" | "footer" | "form" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "head" | "header" | "hgroup" | "hr" | "html" | "i" | "iframe" | "img" | "input" | "ins" | "kbd" | "keygen" | "label" | "legend" | "li" | "link" | "main" | "map" | "mark" | "menu" | "menuitem" | "meta" | "meter" | "nav" | "noscript" | "object" | "ol" | "optgroup" | "option" | "output" | "p" | "param" | "picture" | "pre" | "progress" | "q" | "rp" | "rt" | "ruby" | "s" | "samp" | "script" | "section" | "select" | "small" | "source" | "span" | "strong" | "style" | "sub" | "summary" | "sup" | "table" | "tbody" | "td" | "textarea" | "tfoot" | "th" | "thead" | "time" | "title" | "tr" | "track" | "u" | "ul" | "var" | "video" | "wbr" | "webview", props?: ClassAttributes<T extends HTMLElement> & P extends HTMLAttributes, ...children: ReactNode[]) => DetailedReactHTMLElement<P extends HTMLAttributes, T extends HTMLElement>; 
    <P extends SVGAttributes<T extends SVGElement>, T extends SVGElement>(type: "animate" | "circle" | "clipPath" | "defs" | "desc" | "ellipse" | "feBlend" | "feColorMatrix" | "feComponentTransfer" | "feComposite" | "feConvolveMatrix" | "feDiffuseLighting" | "feDisplacementMap" | "feDistantLight" | "feDropShadow" | "feFlood" | "feFuncA" | "feFuncB" | "feFuncG" | "feFuncR" | "feGaussianBlur" | "feImage" | "feMerge" | "feMergeNode" | "feMorphology" | "feOffset" | "fePointLight" | "feSpecularLighting" | "feSpotLight" | "feTile" | "feTurbulence" | "filter" | "foreignObject" | "g" | "image" | "line" | "linearGradient" | "marker" | "mask" | "metadata" | "path" | "pattern" | "polygon" | "polyline" | "radialGradient" | "rect" | "stop" | "svg" | "switch" | "symbol" | "text" | "textPath" | "tspan" | "use" | "view", props?: ClassAttributes<T extends SVGElement> & P extends SVGAttributes, ...children: ReactNode[]) => ReactSVGElement; 
    <P extends DOMAttributes<T extends Element>, T extends Element>(type: string, props?: ClassAttributes<T extends Element> & P extends DOMAttributes, ...children: ReactNode[]) => DOMElement<P extends DOMAttributes, T extends Element>; 
    <P>(type: SFC, props?: Attributes & P, ...children: ReactNode[]) => SFCElement<P>; 
    <P>(type: ClassType, props?: ClassAttributes<ClassicComponent<P, ComponentState>> & P, ...children: ReactNode[]) => CElement; 
    <P, T extends Component<P, ComponentState>, C extends ComponentClass<P>>(type: ClassType, props?: ClassAttributes<T extends Component> & P, ...children: ReactNode[]) => CElement; 
    <P>(type: StatelessComponent<P> | ComponentClass<P> | string, props?: Attributes & P, ...children: ReactNode[]) => ReactElement<P>; 
}

最佳答案

您可以使用构造函数并返回:

const Element = React.createElement(Vtm.Notification, notificationProps, null);

return Element;

或者你使用JSX来美化构造函数:

return <Vtm.Notification {...notificationProps} />;

createElement

JSX

关于javascript - 在组件内使用 React.createElement 的 TypeScript 无法工作/编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49241714/

相关文章:

javascript - 如何在javascript中一一显示innerHTML值

javascript - Jquery 根据 IF 修改 URL?

reactjs - 下拉 onchange 不起作用 : React+ Typescript+ semantic-ui-react

javascript - 将 fp-ts TaskEither 与右侧的 Either 链接起来

TypeScript 禁止使用 With 语句

javascript - 动态内容加载 View

javascript - 当存在 Parsley 错误 div 时,带有国际电话输入的标志向下移动

javascript - 如何分离相互依赖的 redux reducer?

javascript - Rollup + React 17 与新的 JSX 转换 - "React is not defined"

Angular 替换方法不适用于字符串