在我的React
,TypeScript
应用程序中,我只需创建一个自定义组件的新实例 - 在我的情况下 - 正在显示通知。
通知的道具是从localStorage
读取的。
由于通知需要对高度进行一些调整,我需要创建一个新实例(因此调用componentDidMount
)。
但是,在我的render
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>;
}
答案 0 :(得分:2)
您要么使用构造函数并返回:
const Element = React.createElement(Vtm.Notification, notificationProps, null);
return Element;
或者您使用JSX来美化构造函数:
return <Vtm.Notification {...notificationProps} />;