Var*_*rma 5 typescript reactjs
我在.tsx文件中使用React.memo()(React typescript)
现在,我将Props的接口声明为:
interface Props {
type?: string;
}
Run Code Online (Sandbox Code Playgroud)
我的组件看起来像:
const Component: React.SFC<Props> = props => {
/// return something;
};
export default memo(Component);
Run Code Online (Sandbox Code Playgroud)
现在,由于type是一个可选的道具,我打算仅在某些时候使用它。
如果我使用我的组件,<Component type="something" />一切都很好。但是如果我在<Component />收到错误时使用它->
输入'{children:string; }”与“ IntrinsicAttributes&Props”类型没有共同的属性。
这似乎是一个非常荒谬的错误,因为我已经在Code沙箱上尝试过此方法,并且一切正常。关于错误可能是什么的任何想法?
更新
如果我在接口中明确添加道具
interface Props {
type?: string;
children?: ReactNode;
}
Run Code Online (Sandbox Code Playgroud)
那么在这种情况下,一切都很好。这应该是隐式的,但要考虑到它的错误
'{儿童:字符串;}'
有任何想法吗???
我相信打字有错误。改变
function memo<P extends object>(
Component: SFC<P>,
propsAreEqual?: (
prevProps: Readonly<PropsWithChildren<P>>,
nextProps: Readonly<PropsWithChildren<P>>
) => boolean
): NamedExoticComponent<P>;
Run Code Online (Sandbox Code Playgroud)
到
function memo<P extends object>(
Component: SFC<P>,
propsAreEqual?: (
prevProps: Readonly<PropsWithChildren<P>>,
nextProps: Readonly<PropsWithChildren<P>>
) => boolean
): NamedExoticComponent<PropsWithChildren<P>>;
Run Code Online (Sandbox Code Playgroud)
解决问题
更新
嗯,实际上这不是一个错误。见讨论。
TLTR:您必须children在组件道具类型中指定。
type MyComponentProps {
children: React.ReactNode;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2329 次 |
| 最近记录: |