我正在尝试将上下文传递到react docs中概述的SFC中,但是打字稿对此感到惊讶。我已在codeandbox.io上重现了该问题,并内联了相关信息。所以-
Chill, everything will be okay
?Property 'contextTypes' does not exist on type '(props: ShirtProps, context: any) => Element'.
component
如下所示:import * as React from 'react';
import * as PropTypes from 'prop-types';
import { ShirtProps as Props } from '../types';
// tslint:disable-next-line:no-any
export const Shirt = (props: Props, context: any) => {
return (
<h1>Example text size is {props.scale}</h1>
);
};
// Typescript throws the following warning:
// Property 'contextTypes' does not exist on type '(props: ShirtProps, context: any) => Element'.
Shirt.contextTypes = {
parallaxController: PropTypes.object.isRequired
};
export default Shirt;
谢谢!
*欢迎使用其他任何提示-我刚刚开始使用Typescript和React。
如果您告诉它Shirt是SFC,则应进行编译(类型为React.SFC<Props>
):
import * as React from 'react';
import * as PropTypes from 'prop-types';
import { ShirtProps as Props } from '../types';
// tslint:disable-next-line:no-any
export const Shirt: React.SFC<Props> = (props: Props, context: any) => {
return (
<h1>Example text size is {props.scale}</h1>
);
};
Shirt.contextTypes = {
parallaxController: PropTypes.object.isRequired
};
export default Shirt;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句