【发布时间】:2018-12-12 10:44:52
【问题描述】:
我无法在 vs 代码中从当前组件渲染另一个组件。我得到的错误信息是..
类型“{}”不可分配给类型“IntrinsicAttributes & IntrinsicClassAttributes & Readonly & ...'。类型“{}”不可分配给类型 '只读>'。 类型“{}”中缺少属性“匹配”。
这是我的代码..
import * as React from 'react';
import { BillList } from './BillList';
import * as ReactDOM from "react-dom";
export interface BillComponentState {
children?: React.ReactNode,
bills: BillList
}
export class BillComponent extends React.Component<BillList>{
public render() {
return <div className='container-fluid'>
<div className='row'>
<div className='col-sm-3'>
<BillList />
</div>
<div className='col-sm-9'>
{this.props.children}
</div>
</div>
</div>;
}
}
为什么我不能只渲染我的 BillList,它实际上是在我的网络服务器上渲染来自 web api 的字符串列表?
免责声明:我对 React 很陌生
编辑:这是 BillList.tsx 中的代码
import * as React from 'react';
import { RouteComponentProps } from 'react-router';
interface BillState {
bills: Bill[],
loading: boolean
}
export class BillList extends React.Component<RouteComponentProps<{}>, BillState>
{
constructor() {
super();
this.state = { bills: [], loading: true };
fetch("api/SampleData/GetBills")
.then(response => response.json() as Promise<Bill[]>)
.then(data => {
this.setState({
bills: data,
loading: false
});
});
}
public render() {
let contents = this.state.loading
? <p><em>Loading...</em></p>
: BillList.renderBillsToList(this.state.bills);
return <div className="rendered-bills">
<h1>Bills to pay</h1>
{contents}
</div>
}
public static renderBillsToList(bills: Bill[]) {
return <ul>
{bills.map((bill, i) =>
<li key={i}> {bill.name} </li>
)}
</ul>;
}
}
interface Bill {
name: string;
}
这是一个公共仓库中的代码。如果您需要我提供更多调试信息,请告诉我。
【问题讨论】:
-
试试这个。导出接口 BillComponentState { children?: React.ReactNode, bills?: BillList }
-
尝试共享最少的复制代码。
-
更改账单:BillList 到 bills?:不幸的是,BillList 仍然给我同样的错误
标签: reactjs typescript .net-core react-router