react-hooks-snippets icon indicating copy to clipboard operation
react-hooks-snippets copied to clipboard

Customize UseState Hook

Open Brian-1150 opened this issue 4 years ago • 1 comments

Is there an option to customize the templates? For example, when I type 'ush + tab' I get: const [ |, set|] = useState();

This is great. Thank you for creating this extension!

But this will not set the 'set' part in camelCase. If I wanted my const to be named 'foo' I would get: const [foo, setfoo] = useState(); instead of const [foo, setFoo] = useState(); but my personal preference is actually const [foo, fooSet] = useState();

Thanks again!

Brian-1150 avatar Sep 30 '21 17:09 Brian-1150

Hey @Brian-1150 , glad you like the extension!

At the moment, the ush snippet only supports const [foo, setFoo] = useState();

We've talked about adding custom settings in https://github.com/alDuncanson/react-hooks-snippets/issues/15, which can be done, but to do so the extension will have to be re-written in typescript.

I plan to do this, but it could take a while.

So, in the meantime I wrote a workaround for you:

  • open the command palette and type Preferences: Configure User Snippets
  • then select New Global Snippets file...
  • paste the following:
"customUseState": {
    "prefix": "<your-snippet-prefix>",
    "body": [
      "const [${1}, ${1}Set] = useState($2);$0"
    ],
    "description": "React useState() hook"
  }
  • replace <your-snippet-prefix> with the prefix you'd like to type to insert the snippet
    • ex useState, customState, etc
  • then save the file and you should be good to go!

Here's an example walkthrough: https://imgur.com/a/7C7GO9p

alDuncanson avatar Oct 01 '21 14:10 alDuncanson

Thanks

Brian-1150 avatar Sep 15 '23 22:09 Brian-1150

Thanks

I no longer have the spare time to add new features to this extension, feel free to open a pr with the changes you'd like to see and I'll be happy to review it!

alDuncanson avatar Sep 18 '23 13:09 alDuncanson