简体   繁体   English

在组件内部使用React.createElement的TypeScript无法正常工作/编译

[英]TypeScript using React.createElement inside component not working/compiling

In my React , TypeScript application I simply need to create new instance of a custom component which - in my case - is showing a notification. 在我的ReactTypeScript应用程序中,我只需要创建一个自定义组件的新实例,就我而言,该实例正在显示通知。

The notification's props are read from the localStorage . 通知的道具从localStorage中读取。 As the notification needs some adjustments regarding the height I need to create a new instance (so componentDidMount gets called). 由于通知需要对高度进行一些调整,因此我需要创建一个新实例(这样会调用componentDidMount )。

However, when calling in my render 但是,在调用render

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

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

return <Element />;

I get a bunch of errors: 我收到很多错误:

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

and from ReSharper: 从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>; 
}

You either use the constructor and return that: 您可以使用构造函数并返回:

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

return Element;

Or you use JSX to beautify the constructor: 或者使用JSX美化构造函数:

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

createElement createElement

JSX JSX

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM