在打字稿中使用immutability-helper并做出反应

时间:2017-07-03 07:58:41

标签: reactjs typescript immutability typescript-typings

我从update导入了@types/immutability-helper。现在我想将项目推送到React的组件状态中的数组。

index.d.ts文件中的语法说:

interface UpdateFunction {
    (value: any[], spec: UpdateArraySpec): any[];
    (value: {}, spec: UpdateSpec): any;
    extend: (commandName: string, handler: CommandHandler) => any;
}

UpdateArraySpec

interface UpdateArraySpec extends UpdateSpecCommand {
    $push?: any[];
    $unshift?: any[];
    $splice?: any[][];
    [customCommand: string]: any;
}

这是否意味着我必须写2次更新?:

this.setState(update(this.state, update(this.state.Markers, { $push: [info] })));

或者什么?

1 个答案:

答案 0 :(得分:2)

这是对我有用的语法:

this.setState(update(this.state, { Markers: { $push: [info] } }));

这在typescript定义文件中并不明显。但是the docs说了类似的东西。

相关问题