我在React项目中使用了可重用组件。该组件传递了一些道具,供我们随意使用,并且是必需的。
我在TypeScript文件中,并且试图将以前的道具从PropTypes转换为TS interface
。
以下是组件以及变形后的Props和界面:
问题出在道具上:callback, actualValue, label
。
interface RouteUserInfo {
username: string;
callBack: string;
actualvalue: string;
label: string;
}
export interface IState {
isLoading: boolean;
user: IUser | null;
errorMessage: HttpResponseObj | null;
}
export class UserDetailsPage extends Component<RouteComponentProps<RouteUserInfo>, IState> {
hasBeenMounted = false;
state: IState = {
errorMessage: null,
isLoading: false,
user: null
};
render(): React.ReactNode {
const { user, isLoading, errorMessage } = this.state;
const { callBack, actualValue, label, match } = this.props;
return (
<div className="container-fluid lenses-container-fluid">
{this.renderNoResourceComponent()}
<div
className="d-flex justify-content-between justify-content-center my-2"
data-test="basic-render"
>
<div className="d-flex flex-row">
<h2>{match.params.username}</h2>
<span
className="ml-3 my-2"
onChange={callBack}
value={actualValue}
placeholder={label}
data-tip
data-event="mouseover"
data-event-off="keydown mouseout"
data-for="loggedIn"
>
{user.isActive ? <ActivityBadge isActive /> : <ActivityBadge />}
</span>
</div>
{user && this.renderHeaderDropdown()}
</div>
</div>
);
}
但是,当尝试在<span />
上这样使用它们时,出现以下错误:
Type '{ children: Element; className: string; onChange: any; value: any; placeholder: any; data-tip: true; data-event: string; data-event-off: string; data-for: string; }' is not assignable to type 'DetailedHTMLProps<HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>'.
Property 'value' does not exist on type 'DetailedHTMLProps<HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>'.ts(2322)
在实际道具上我得到了:
用于回调
Property 'callBack' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<RouteComponentProps<RouteUserInfo, StaticContext, any>>'.ts(2339)
const callBack: any
对于实际值:
Property 'actualValue' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<RouteComponentProps<RouteUserInfo, StaticContext, any>>'.ts(2339)
const actualValue: any
对于标签:
Property 'label' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<RouteComponentProps<RouteUserInfo, StaticContext, any>>'.ts(2339)
const label: any
我显然可以对所有这些属性使用:any
,但是我想知道它们的特定类型以及如何找到它们的方法。
我还提供了可重用组件本身:
import React from 'react';
import ReactTooltip, { Place } from 'react-tooltip';
import './styles.scss';
export interface Props {
id: string;
place?: Place;
children?: React.ReactNode;
}
function Tooltip(props: Props): JSX.Element {
const { children, id, place, ...rest } = props;
return (
<ReactTooltip id={id} place={place} {...rest} className="tooltip">
{children}
</ReactTooltip>
);
}
export default Tooltip;
答案 0 :(得分:0)
为什么要向Path.toAbsolutePath().toString()
提供value
,onChange
和placeholder
?如果您的跨度是某种受控输入,那么您可能应该考虑将其切换为实际的span
元素,这样就不会抱怨这些道具了:)