Reactjs 设置具有多个项的对象的状态
我试图设置具有以下特征的对象状态:Reactjs 设置具有多个项的对象的状态,reactjs,typescript,Reactjs,Typescript,我试图设置具有以下特征的对象状态: interface GridValue { control: string; rowIndex: number; cellIndex: number; } “我的组件状态”包含以下内容: interface ComponentState { results: InChartModel[]; loading: boolean; control: string; modalValue: GridValue[
interface GridValue {
control: string;
rowIndex: number;
cellIndex: number;
}
“我的组件状态”包含以下内容:
interface ComponentState {
results: InChartModel[];
loading: boolean;
control: string;
modalValue: GridValue[];
}
但是,当我尝试使用以下代码设置状态时,它不起作用。属性类型modalValue不兼容:
handleClick(e: any) {
const rindex: number = e.currentTarget.rowIndex;
const cindex: number = e.target.cellIndex;
const grid: any = e.currentTarget.parentNode.parentNode;
const ctrl: string = grid.rows[rindex].cells[0].innerText;
const metric: string = grid.rows[rindex].cells[1].innerText;
this.setState({
modalValue: {
"control": ctrl,
"rowIndex": rindex,
"cellIndex": cindex
}
})
this.props.getInptModal(ctrlType, metric);
}
这是如何正确地做到这一点的粗略想法吗?我已经能够用单个变量设置状态,但我正在努力用对象更新状态。modalValue需要一个GridValue数组,而不仅仅是一个GridValue。您可以传入GridValue对象数组,也可以将ComponentState的modalValue设置为GridValue:
您将modalValue描述为数组,但将其设置为对象。
将其类型更改为GridValue或在setState上使用数组:
interface ComponentState {
results: InChartModel[];
loading: boolean;
control: string;
modalValue: GridValue; // not an array
}
this.setState({
modalValue: [{
control: ctrl,
rowIndex: rindex,
cellIndex: cindex
}]
})