Reputation: 8265
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
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