Mahmood Dehghan
Mahmood Dehghan

Reputation: 8265

Using immutability-helper in typescript and react

I imported update from @types/immutability-helper. Now I want to push an item to the an array in a React's component state.

The syntax in index.d.ts file says:

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

and UpdateArraySpec is

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

does it mean I have to write 2 updates?:

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

Or what?

Upvotes: 1

Views: 2524

Answers (1)

Mahmood Dehghan
Mahmood Dehghan

Reputation: 8265

This is the syntax that worked for me:

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

This is not obvious from typescript definition file. But the docs says something like it.

Upvotes: 2

Related Questions