1

In my React, TypeScript application I simply need to create new instance of a custom component which - in my case - is showing a notification.

The notification's props are read from the localStorage. As the notification needs some adjustments regarding the height I need to create a new instance (so componentDidMount gets called).

However, when calling in my 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:

_$_$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>; 
}
1
  • why don't you go with JSX element? Commented Mar 12, 2018 at 18:09

1 Answer 1

2

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:

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

createElement

JSX

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.