Home > database >  Why this setState is like this and how this is working?
Why this setState is like this and how this is working?

Time:11-02

Sorry I don't know how to search this kinda problem and I didn't find any answer to this but I'm so confused with this code, its a setState function for an OTP verification :

setCode((currCode) =>
  currCode
  .split('')
  .map((ch, i) => (i !== idx ? ch : isCharNumber ? val : isEmpty ? '_' : ch))
  .join(''),
);

I understand the map part I'm just wondering about that comma "," after the join function. why? if I delete that the code works but ESlint tell me to put that at the end. does it return an object? it just goes through the state which is "____" (four underscores) and change each _ to digit. I really appreciate if someone explain this to me and tell me how can I search and look for this kind of confusions. ty

CodePudding user response:

does it return an object?

No.

I'm just wondering about that comma "," after the join function. why?

In modern JavaScript, it's optional to have a comma after the last argument in a function call (and after the last parameter in a function parameter list), like this:

example(
    firstArg,
    secondArg,
    thirdarg,
  //        ^−−−−−−−−−−−− optional
)

It makes it convenient when adding/removing arguments later not to have to worry about adding/removing a comma.

That's what's going on in your code, there's a single argument to setCode (the arrow function), followed by a comma.

if I delete that the code works but ESlint tell me to put that at the end.

ESLint can be configured to require trailing commas in function argument lists; apparently it's configured that way in your setup. If you don't like the rule, you can turn it off (in private projects) or discuss it with your team (in team projects).

  • Related