【发布时间】:2018-08-01 10:59:27
【问题描述】:
我正在将 JavaScript 中的现有代码重写为 TypeScript。但是,我是 React Native 的新手。我的问题是:
我有一个自定义Component 和自定义State 接口。我想通过自定义State 接口来解决我现在在组件本身内部遇到的错误。这是我的接口代码:
interface MeterReadingStaticState {
process: any,
selected: {
value: any,
isAccepted: boolean,
type: any,
},
isLoaded: boolean,
}
这是我Component的内容:
export default class MeterReadingStatic extends Component<
MeterReadingStaticProps, MeterReadingStaticState> {
state: MeterReadingStaticState = {
selected: {
value: '',
isAccepted: false,
type: '',
},
process: [],
isLoaded: false
};
onAcceptPress = () => {
const updatedProcess = this._updateObj('isAccepted', true);
this.setState({ process: updatedProcess });
};
_updateObj = (prop: MeterReadingStaticProps, value: any) => {
const { process, selected } = this.state;
const target = process.filter(
item => item.name === selected.name && item.type === selected.type
)[0];
target[prop] = value;
const updatedProcess = process.map(
item =>
item.name === target.name && item.type === target.type ? target : item
);
return updatedProcess;
};
从上面的代码可以看出,我想访问状态属性的具体参数。但是,当我尝试这样做时,我遇到了一堆错误。当我尝试初始化我的State 以及尝试在onAcceptPress 函数中使用_updateObj 时,我也会遇到一些错误。
在定义State 的接口时,我还没有找到任何关于这种特定情况的资料。有什么解决方案吗?
【问题讨论】:
-
您遇到了哪些具体错误?
-
@TitianCernicova-Dragomir 属性“类型”在类型“MeterReadingStaticState”上不存在。