1

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.