19

I'm trying to create a VS code user snippet for useState()

Currently I have

  "use state": {
    "prefix": "us",
    "body": [
      "const [$1, set${1/(.*)/${1:/upcase}/}] = useState($2);",
      "$3"
    ],
    "description": "creates use state"
  },

When I enter 'foo' at $1 (position 1) I get:

const [foo, setFOO] as useState()

However I would like to get:

const [foo, setFoo] as useState()

How do I change my snippet to work this way?

AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
ace2case
  • 261
  • 2
  • 8

2 Answers2

24

You just need to change your transform to capitalize like:

"const [$1, set${1/(.*)/${1:/capitalize}/}] = useState($2);",

Mark
  • 143,421
  • 24
  • 428
  • 436
  • As per [this comment](https://stackoverflow.com/questions/65846228/capitalize-first-letter-of-vscode-snippet#comment123161116_65846807), in case you're wondering why the 'set' part isn't capitalized, you have to press `tab` for the capitalization to happen :) – Yuan-Hao Chiang Jul 31 '23 at 22:23
  • Tab twice, in my experience – MayTheSForceBeWithYou Aug 21 '23 at 07:35
0

You need to remove the "*", this character transforms all others to uppercase, without it, you will leave only the first uppercase.

  "prefix": "us",
  "body": [
    "const [$1, set${1/(.)/${1:/upcase}/}] = useState($2);",
    "$3"
   ],
  "description": "creates use state"
},
Kotana Sai
  • 1,207
  • 3
  • 9
  • 20