Home > Back-end >  Unexpected use of comma operator
Unexpected use of comma operator

Time:05-11

I have following error message:

Unexpected use of comma operator no-sequences

This is the code:

<Icon.Clipboard onClick={() => (

    this.handleModal("open","modify"), this.prettyPrint(JSON.stringify(res)))}
/>

Where does the problem come from?

CodePudding user response:

It's the , after this.handleModal("open","modify"). That arrow function should use a full function body ({ after the => and its accompanying } at the end) and a statement separator (; or newline) between the two function calls:

<Icon.Clipboard onClick={() => {
    this.handleModal("open","modify");
    return this.prettyPrint(JSON.stringify(res));
}} />

Or more likely, you don't want that return (but that's what your code using the comma operator was doing):

<Icon.Clipboard onClick={() => {
    this.handleModal("open","modify");
    this.prettyPrint(JSON.stringify(res));
}} />

The comma operator evaluates its left-hand operand, then throws that result away, evalutes its right-hand operand, and takes that as its result. It's commonly abused as a statement separator, but it isn't one and doing so can have unintended consequences (such as returning the result of printPrint above — probably harmless in this case, but often not).

  • Related