简体   繁体   English

React typescript 条件渲染类型日期

[英]React typescript conditional rendering typeof Date

I'm trying to adapt the example from MaterialUI Table Fixed header for my project.我正在尝试为我的项目改编 MaterialUI Table Fixed header 中的示例。 I have a Date field.我有一个日期字段。 That's the problem.那就是问题所在。

I get the error message: TS2367: This condition will always return 'false' since the types 'string' and 'DateConstructor' have no overlap.我收到错误消息:TS2367:此条件将始终返回 'false',因为类型 'string' 和 'DateConstructor' 没有重叠。

<TableRow hover role="checkbox" tabIndex={-1} key={row.RECORDNO}>
    {columns.map((column) =>{
        const value = row[column.id];

        return(
            <TableCell key={column.id} align={column.align}>
                {column.formatFld && typeof value === Date ? column.formatFld(value) : value}
                                     {/*^^^^^^^^^^^^^^^^^^^^^^ Error*/}
            </TableCell>
        );
    })}
</TableRow>

Does anyone have any idea how I can work around this?有谁知道我该如何解决这个问题?

You can't check whether a value is a date this way:您无法以这种方式检查值是否为日期:

value = new Date()
console.log(typeof value) // "object" 

do it like this:像这样做:

console.log(value.constructor.name === "Date") // true

Actually, the typeof operator only tells apart primitive types ( string , number , bigint , boolean , symbol , undefined , object and function ).实际上, typeof运算符只区分原始类型( stringnumberbigintbooleansymbolundefinedobjectfunction )。 To check if a variable is an instance of a class use the instanceof operator.要检查变量是否是 class 的实例,请使用instanceof运算符。

<TableRow hover role="checkbox" tabIndex={-1} key={row.RECORDNO}>
  {columns.map((column) => {
    const value = row[column.id];
  
    return (
      <TableCell key={column.id} align={column.align}>
        {column.formatFld && value instanceof Date ? column.formatFld(value) : value}
      </TableCell>
    );
  })}
</TableRow>

This is my interface and this is how the data is output.这是我的界面,这就是数据是 output 的方式。

export interface IAnwesenheit {
    DATUM: Date,
    NAMEN: string,
    VORNAME: string,
    FIRMA: string,
    PERSONALID: number,
    FIRMAID: number,
    STARTZEIT: Date,
    ENDEZEIT: Date,
    BARCODEIDENT: string,
    BAUSTELLE: string,
    BAUSTELLEID: number,
    RECORDNO: number,
}

BARCODEIDENT: "Z"
BAUSTELLE: "München"
BAUSTELLEID: 4
DATUM: "2017-06-12T00:00:00.000+02:00"
ENDEZEIT: "1899-12-30T18:09:19.000+01:00"
FIRMA: "XY GmbH"
FIRMAID: 3
NAMEN: "Firma 1"
PERSONALID: 7
RECORDNO: 1
STARTZEIT: "1899-12-30T07:15:07.000+01:00"
VORNAME: "Person"

Many thanks, I implemented it now and it works.非常感谢,我现在实现了它并且它有效。

<TableRow hover role="checkbox" tabIndex={-1} key={row.RECORDNO}>
    {columns.map((column) => {

        const value = row[column.id];

        return (
            <TableCell key={column.id} align={column.align}>
                {column.formatFld && (value as Date) ? column.formatFld(new Date(value as Date)) : value}
            </TableCell>
        );
    })}
</TableRow>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM