Reactjs Can';t为react数据网格设置列。给出不可赋值类型错误
我对React和TypeScript都是新手,我正在尝试根据这里的简单示例制作一个简单的React数据网格: 以下是我所拥有的:Reactjs Can';t为react数据网格设置列。给出不可赋值类型错误,reactjs,react-data-grid,Reactjs,React Data Grid,我对React和TypeScript都是新手,我正在尝试根据这里的简单示例制作一个简单的React数据网格: 以下是我所拥有的: import React from 'react'; import ReactDataGrid from 'react-data-grid'; const columns = [ { key: "id", name: "ID", editable: true }, { key: "title", name: "Title", editable: tr
import React from 'react';
import ReactDataGrid from 'react-data-grid';
const columns = [
{ key: "id", name: "ID", editable: true },
{ key: "title", name: "Title", editable: true },
{ key: "complete", name: "Complete", editable: true }
];
const rows = [
{ id: 0, title: "Task 1", complete: 20 },
{ id: 1, title: "Task 2", complete: 40 },
{ id: 2, title: "Task 3", complete: 60 }
];
export const DataTable: React.FC = () => {
return (
<ReactDataGrid
columns={columns}
rowGetter={i => rows[i]}
rowsCount={rows.length}
/>
);
};
从“React”导入React;
从“react数据网格”导入ReactDataGrid;
常量列=[
{key:“id”,name:“id”,可编辑:true},
{key:“title”,name:“title”,可编辑:true},
{key:“complete”,name:“complete”,可编辑:true}
];
常量行=[
{id:0,标题:“任务1”,完成时间:20},
{id:1,标题:“任务2”,完成时间:40},
{id:2,标题:“任务3”,完成时间:60}
];
导出常量数据表:React.FC=()=>{
返回(
行[i]}
rowsCount={rows.length}
/>
);
};
但是,我在设置表中的列的链接上不断遇到错误。它说:
(JSX attribute) DataGridProps<{ [x: string]: {}; }, React.ReactText>.columns: (ColumnValue<{
[x: string]: {};
}, unknown, string> | ColumnValue<{
[x: string]: {};
}, unknown, number>)[]
Type '{ key: string; name: string; editable: boolean; }[]' is not assignable to type '(ColumnValue<{ id: number; title: string; complete: number; }, unknown, "id"> | ColumnValue<{ id: number; title: string; complete: number; }, unknown, "title"> | ColumnValue<{ id: number; title: string; complete: number; }, unknown, "complete">)[]'.
Type '{ key: string; name: string; editable: boolean; }' is not assignable to type 'ColumnValue<{ id: number; title: string; complete: number; }, unknown, "id"> | ColumnValue<{ id: number; title: string; complete: number; }, unknown, "title"> | ColumnValue<{ id: number; title: string; complete: number; }, unknown, "complete">'.
Type '{ key: string; name: string; editable: boolean; }' is not assignable to type 'ColumnValue<{ id: number; title: string; complete: number; }, unknown, "complete">'.
Types of property 'key' are incompatible.
Type 'string' is not assignable to type '"complete"'.ts(2322)
DataGrid.d.ts(6, 5): The expected type comes from property 'columns' which is declared here on type 'IntrinsicAttributes & DataGridProps<{ id: number; title: string; complete: number; }, "id" | "title" | "complete"> & { ref?: ((instance: DataGridHandle | null) => void) | RefObject<...> | null | undefined; }'
(JSX属性)DataGridProps.columns:(ColumnValue | ColumnValue)[]
类型“{key:string;name:string;可编辑:boolean;}[]”不可分配给类型“(ColumnValue | ColumnValue | ColumnValue)[]”。
类型“{key:string;name:string;editable:boolean;}”不能分配给类型“ColumnValue | ColumnValue | ColumnValue”。
类型“{key:string;name:string;可编辑:boolean;}”不可分配给类型“ColumnValue”。
属性“key”的类型不兼容。
类型“string”不可分配给类型“complete”。ts(2322)
DataGrid.d.ts(6,5):预期的类型来自属性“columns”,它在这里的类型“intrinsifictattributes&DataGridProps&{ref:”((实例:DataGridHandle | null)=>void)|重新对象| null |未定义;}上声明
有人能帮我分析这个错误吗?是否需要将列的类型显式设置为react数据网格所期望的类型?我安装了react数据网格类型,错误消失了:
npm install --save @types/react-data-grid
尝试删除
:React.FC
@HamedNavabian你能解释一下原因吗?如果我想让它成为网格的包装器组件呢?这是一个测试。我阅读了文档,看到了一个简单的js示例。也尝试不同的模式,但我没有得到错误@哈梅德纳瓦比安我很抱歉,但这是没有帮助的。我的理解是,功能组件不管理状态。在上面的例子中,它根本不管理状态,那么为什么它不工作呢?是否可能是ReactDataGrid本身管理状态,而不能位于FunctionalComponent中?是否尝试删除该包并重新安装?