意外使用逗号运算符

Unexpected use of comma operator

我收到以下错误消息:

Unexpected use of comma operator no-sequences

这是代码:

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

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

问题出在哪里?

this.handleModal("open","modify")之后的,。该箭头函数应使用完整的函数体(=> 之后的 { 及其末尾随附的 } 和两者之间的语句分隔符(; 或换行符)函数调用:

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

或者更可能的是,您不希望 return(但这正是您使用逗号运算符的代码所做的):

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

逗号运算符计算其 left-hand 操作数,然后丢弃该结果,计算其 right-hand 操作数,并将其作为结果。它通常被滥用为语句分隔符,但它不是一个分隔符,这样做会产生意想不到的后果(例如返回上面 printPrint 的结果——在这种情况下可能无害,但通常不会)。