简体   繁体   English

输入'{数据:InvitedUser[]; ““: 任何; }' 不可分配给类型 'Element'

[英]Type '{ data: InvitedUser[]; “”: any; }' is not assignable to type 'Element'

I'm having a typescript issue that i need some assistance with if possible.我遇到了 typescript 问题,如果可能的话,我需要一些帮助。

I have a parent component that passes in an array of results through to a child component that I will then map to display the info.我有一个父组件将结果数组传递给子组件,然后我将 map 显示信息。

Parent Component:父组件:

import { Table } from 'semantic-ui-react';
import { InvitedUser } from '../common/types/userInvitations';
import EmptyUserInvitesTable from './EmptyUserInvitesTable';
import UserInvitesTable from './UserInvitesTable';

// Type that sets up the prop types for props passed into
// the component
type UserInvitationsProps = {
  data: Array<InvitedUser>;
};

// Class which contains the User Signup form and keeps the values in state, ready to be sent to the API.
class UserInvitations extends Component<
UserInvitationsProps
> {
  render(): JSX.Element {
    const { data } = this.props;

    let showData = false;

    if (data) {
      showData = true;
    }

    return (
      <div className="row settings-column">
        <div className="userInvitesWidth">
          <div className="row form-title align-column">
            <Table striped>
              <Table.Header>
                <Table.Row>
                  <Table.HeaderCell />
                  <Table.HeaderCell>Invitee</Table.HeaderCell>
                  <Table.HeaderCell>Email Address</Table.HeaderCell>
                  <Table.HeaderCell>Invited By</Table.HeaderCell>
                  <Table.HeaderCell>Email Address</Table.HeaderCell>
                  <Table.HeaderCell>Date Invited</Table.HeaderCell>
                  <Table.HeaderCell>Date Joined</Table.HeaderCell>
                  <Table.HeaderCell>Actions</Table.HeaderCell>
                </Table.Row>
              </Table.Header>

              {showData
                ? (
                  <EmptyUserInvitesTable />
                )
                : (
                  <UserInvitesTable data={data}/>
                )}
            </Table>
          </div>
        </div>
      </div>
    );
  }
}

export default UserInvitations;

Child Component:子组件:

import { Table, Image, Button } from 'semantic-ui-react';
import { InvitedUser } from '../common/types/userInvitations';
import styled from 'styled-components';

const SendEmailButton = styled(Button)`
  width: 72px;
  height: 36px;
  border-radius: 18px;
  border: solid 1px #4c4cdc;
  background-color: #ffffff;
  color: #4a4a4a;
  font-family: "Roboto", sans-serif !important;
  font-size: 14px !important;
`;

// Type that sets up the prop types for props passed into
// the component
type UserInvitationTableProps = {
  data: Array<InvitedUser>;
};

class UserInvitationsTable extends Component<
UserInvitationTableProps
> {
  render(): JSX.Element {
    const { data } = this.props;

    return (
      {data.map((dataset: any) => (
        <Table.Body>
          <Table.Row key={dataset.id}>
            <Table.Cell>
              <Image src={dataset.invitedBy.avatar} rounded size="mini" />
            </Table.Cell>
            {dataset.firstName && dataset.lastName
              ? (
                <Table.Cell>
                  {dataset.firstName}
                  {dataset.lastName}
                </Table.Cell>
              )
              : (
                <Table.Cell>N/A</Table.Cell>
              )}
            <Table.Cell>{dataset.username}</Table.Cell>
            <Table.Cell>{dataset.invitedBy.name}</Table.Cell>
            <Table.Cell>{dataset.invitedBy.username}</Table.Cell>
            <Table.Cell>{dataset.createdOn}</Table.Cell>
            {dataset.invitedState === 'ARCHIVED'
              ? (
                <Table.Cell>{dataset.updatedOn}</Table.Cell>
              )
              : (
                <Table.Cell>N/A</Table.Cell>
              )}
            {dataset.invitedState === 'PENDING_USER_ACCEPTATION'
              ? (
                <Table.Cell>
                  <SendEmailButton
                      className="sumBtn"
                      // onClick={(e) => this.onStart(e, range)} need to work out what the button does from igor
                      // disabled={disabled}
                      // loading={startLoading} TBC
                    >
                    Start
                  </SendEmailButton>
                </Table.Cell>
              )
              : (
                <Table.Cell>No Actions</Table.Cell>
              )}
          </Table.Row>
        </Table.Body>
    ))}
  )}
}

export default UserInvitationsTable;

I'm getting the error我收到错误

Type '{ data: InvitedUser[];输入'{数据:InvitedUser[]; "": any; ““: 任何; }' is not assignable to type 'Element'. }' 不可分配给类型“元素”。

But not sure why.但不确定为什么。 Any help would be appreciated.任何帮助,将不胜感激。

Thanks谢谢

From highest component remove : ReactNode从最高组件移除:ReactNode

const CUserInvitations: React.FC<{}> = (): JSX.Element => (
  <Query query={INVITED_USERS}>
    {({ loading, data }: QueryResult<Array<InvitedUser>>): ReactNode => {
      if (loading) {
        return null;
      }

      if (!data) return null;

      return <UserInvitations data={data} />;
    }}
  </Query>
);

Highest Component:最高成分:

import { Query, QueryResult } from 'react-apollo';
import UserInvitations from '../components/userInvitations/UserInvitations';
import { InvitedUser } from '../components/common/types/userInvitations';
import { INVITED_USERS } from '../components/common/Queries';

/**
 * the container for the user invitations
 * renders the expense page wrapped by a query.
 */
const CUserInvitations: React.FC<{}> = (): JSX.Element => (
  <Query query={INVITED_USERS}>
    {({ loading, data }: QueryResult<Array<InvitedUser>>): ReactNode => {
      if (loading) {
        return null;
      }

      if (!data) return null;

      return <UserInvitations data={data} />;
    }}
  </Query>
);

export default CUserInvitations;

Exported type for data:数据的导出类型:

  firstName: string;
  lastName: string;
  createdOn: string;
  username: string;
  updatedOn: string;
  id: string;
  invitedState: string;
  invitedBy: {
    name: string;
    avatar: string;
    id: string;
    username: string;
  }
};```

输入'元素| undefined' 不可分配给类型 'ReactElement <any, string | ((props: any)< div><div id="text_translate"><p> 我有一个看起来像这样的组件。 这个版本完美运行:</p><pre> export default function StatusMessage(isAdded: boolean, errorMessage: string) { if (isAdded) { return <ResultAlert severity="success" text="User Added" />; } else { if (errorMessage.== '') { if ( errorMessage;includes('email') ) { return <ResultAlert severity="error" />. } if ( errorMessage;includes('phone number') ) { return ( <ResultAlert severity="error" text="" /> ); } } } }</pre><p> 现在,我正试图改变我导出它的方式。 我正在尝试这个:</p><pre> type StatusMessageProps = { isAdded: boolean; errorMessage: string; } export const StatusMessage: React.FunctionComponent<StatusMessageProps> = ({ isAdded, errorMessage }) => {</pre><p> 但我不断收到错误消息:</p><pre> Type '({ isAdded, errorMessage }: PropsWithChildren<StatusMessageProps>) => Element | undefined' is not assignable to type 'FunctionComponent<StatusMessageProps>'. Type 'Element | 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'.</pre><pre> I am using the same method on different pages but the error is only here</pre><p> 编辑:</p><p> 我像这样使用这个组件:</p><pre> const [isAdded, setIsAdded] = useState(false); {isSubmitted && StatusMessage(isAdded, errorMessage)}</pre><p> 它在 isAdded 上给我一个错误</p><pre>Argument of type 'boolean' is not assignable to parameter of type 'PropsWithChildren<StatusMessageProps>'.ts(2345)</pre></div></any,> - Type 'Element | undefined' is not assignable to type 'ReactElement<any, string | ((props: any)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript:类型“Element[]”不可分配给类型“ReactElement” <any, string | jsxelementconstructor<any> >'</any,> - Typescript: Type 'Element[]' is not assignable to type 'ReactElement<any, string | JSXElementConstructor<any>>' 输入'元素| undefined' 不可分配给类型 'ReactElement <any, string | ((props: any)< div><div id="text_translate"><p> 我有一个看起来像这样的组件。 这个版本完美运行:</p><pre> export default function StatusMessage(isAdded: boolean, errorMessage: string) { if (isAdded) { return <ResultAlert severity="success" text="User Added" />; } else { if (errorMessage.== '') { if ( errorMessage;includes('email') ) { return <ResultAlert severity="error" />. } if ( errorMessage;includes('phone number') ) { return ( <ResultAlert severity="error" text="" /> ); } } } }</pre><p> 现在,我正试图改变我导出它的方式。 我正在尝试这个:</p><pre> type StatusMessageProps = { isAdded: boolean; errorMessage: string; } export const StatusMessage: React.FunctionComponent<StatusMessageProps> = ({ isAdded, errorMessage }) => {</pre><p> 但我不断收到错误消息:</p><pre> Type '({ isAdded, errorMessage }: PropsWithChildren<StatusMessageProps>) => Element | undefined' is not assignable to type 'FunctionComponent<StatusMessageProps>'. Type 'Element | 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'.</pre><pre> I am using the same method on different pages but the error is only here</pre><p> 编辑:</p><p> 我像这样使用这个组件:</p><pre> const [isAdded, setIsAdded] = useState(false); {isSubmitted && StatusMessage(isAdded, errorMessage)}</pre><p> 它在 isAdded 上给我一个错误</p><pre>Argument of type 'boolean' is not assignable to parameter of type 'PropsWithChildren<StatusMessageProps>'.ts(2345)</pre></div></any,> - Type 'Element | undefined' is not assignable to type 'ReactElement<any, string | ((props: any) “Element”类型的参数不可分配给“ReactElement”类型的参数<any></any> - Argument of type 'Element' is not assignable to parameter of type 'ReactElement<any> 类型“数字”不可分配给类型“元素” - Type 'number' is not assignable to type 'Element' 获取“类型不可分配给类型 FunctionComponent<props> “和”类型缺少类型“React Element”中的以下属性<any, any> '</any,></props> - Getting a "Type is not assignable to type FunctionComponent<Props>" and "Type is missing the following properties from type 'React Element<any, any>' JSX 元素类型 &#39;ReactElement<any> 不是 JSX 元素的构造函数。 类型 &#39;undefined&#39; 不能分配给类型 &#39;Element | 空值&#39; - JSX element type 'ReactElement<any> is not a constructor function for JSX elements. Type 'undefined' is not assignable to type 'Element | null' 输入&#39;NodeListOf <HTMLLIElement> &#39;不可分配给&#39;Element []&#39;类型 - type 'NodeListOf<HTMLLIElement>' is not assignable to type 'Element[]' 错误 -::Type 'string | 元素'不可分配给类型'字符串'。? - Error -::Type 'string | Element' is not assignable to type 'string'.? 类型“未知”不可分配给类型“任何[] | 可迭代的<any> | (可迭代<any> & 任何[])</any></any> - Type 'unknown' is not assignable to type 'any[] | Iterable<any> | (Iterable<any> & any[]) 类型参数不可分配给 ObservableInput 类型<any> - Argument of type is not assignable to type ObservableInput<any>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM