输入错误:JSX元素类型'{} | null | undefined'不是JSX元素的构造函数

问题描述 投票:1回答:1

我有一个名为HandleQuery的React组件来处理Apollo GraphQL查询的结果:

import React, { ReactNode } from 'react';

import { CenteredMessage } from './centered-message';

export interface HandleQueryProps {
    loading: boolean,
    error?: Error,
    children: ReactNode
}

export const HandleQuery = ({ loading, error, children }: HandleQueryProps) => {
    if (loading) {
        return <CenteredMessage>Loading...</CenteredMessage>;
    }

    if (error) {
        return <CenteredMessage>{error.message}</CenteredMessage>;
    }

    return children;
};

当此组件在另一个组件中使用时,它不会编译:

import React from 'react';

import { useQuery } from 'react-apollo-hooks';
import gql from 'graphql-tag';
import { HandleQuery } from '..';
import { MutationType } from '../../graphql-types';
import { AuthorsPanel } from './authors-panel';
import { GET_AUTHORS } from './authors-queries';
import { AuthorMutated } from './__generated__/AuthorMutated';

export class AuthorsContainer extends React.Component {
    render() {
        const { loading, error, data } = useQuery(GET_AUTHORS);
        return (
            <!-- THIS LINE PRODUCES A COMPILER ERROR -->
            <HandleQuery loading={loading} error={error}>
                <AuthorsPanel data={data} />
            </HandleQuery>
        );
    }
}

上面使用HandleQuery会产生以下错误:

输入错误:JSX元素类型'{} | null | undefined'不是JSX元素的构造函数。类型'undefined'不能分配给'Element |类型空值'。 TS2605

这是什么意思,我怎么能摆脱它?

更新

将AuthorsContainer转换为函数组件并不能消除错误:

export const AuthorsContainer = () => {
    const { loading, error, data } = useQuery(GET_AUTHORS);
    return (
        <HandleQuery loading={loading} error={error}>
            <AuthorsPanel data={data} />
        </HandleQuery>
    );
};

更新2来自@FredyC的已实施建议:

import React from 'react';

import { CenteredMessage } from './centered-message';

export interface HandleQueryProps {
    loading: boolean;
    error?: Error;
}

export const HandleQuery: React.FC<HandleQueryProps> = ({
    loading,
    error,
    children
}) => {
    if (loading) {
        return <CenteredMessage>Loading...</CenteredMessage>;
    }

    if (error) {
        return <CenteredMessage>{error.message}</CenteredMessage>;
    }

    return children;
};

现在容器组件上的错误消失了,但HandleQuery组件上出现了一个新的编译器错误:

Type error: Type '({ loading, error, children }: PropsWithChildren<HandleQueryProps>) => {} | null | undefined' is not assignable to type 'FunctionComponent<HandleQueryProps>'.
  Type '{} | null | undefined' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null'.
    Type 'undefined' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null'.  TS2322
reactjs typescript react-apollo
1个回答
4
投票

关于ReactNode存在长期问题。我不明白具体细节,但它是TypeScript本身的硬连线,正在进行中。

无论哪种方式,解决方案应该很容易。不要将ReactNode与功能组件一起使用:) children prop隐式包含在React.FC类型中。

返回的孩子也有同样的问题。它可以通过包装到<React.Fragment><div>来克服,如果你愿意,但由于它只是一个类型错误,你可以说服TypeScript你知道你在做什么:)

import React, { ReactNode } from 'react';

import { CenteredMessage } from './centered-message';

export interface HandleQueryProps {
    loading: boolean,
    error?: Error,
}

export const HandleQuery: React.FC<HandleQueryProps> = ({ loading, error, children }) => {
    if (loading) {
        return <CenteredMessage>Loading...</CenteredMessage>;
    }

    if (error) {
        return <CenteredMessage>{error.message}</CenteredMessage>;
    }

    return children as ReactElement<any>;
};

0
投票

在这种情况下,如果我没有消除某些东西,可以更容易地简单地定义children类型的ReactElement

export interface HandleQueryProps {
    loading: boolean,
    error?: ApolloError,
    children: ReactElement
}
© www.soinside.com 2019 - 2024. All rights reserved.