VSCode snippet: React Reducer

manuartero

Manuel Artero Anguita šŸŸØ

Posted on September 16, 2022

VSCode snippet: React Reducer

No matter how many times I visit the React docs... I always doubt which is the skeleton for reducer functions.

There are VSCode extensions that do this exact thing. the thing is, I have already installed tons of extensions so, there is a code snippet instead:

Just open Snippets: Configure User Snippets and add the following section to the JSON:

    "react-reducer": {
        "scope": "javascriptreact,typescriptreact",
        "prefix": "\\reducer",
        "body": [
            "function reducer(state, action) {",
            "\tswitch(action.type) {",
            "\t\tcase \"${1:foo}\": {",
            "\t\t\treturn {",
            "\t\t\t\t...state,",
            "\t\t\t\tfoo: action.foo,",
            "\t\t\t};",
            "\t\t}",
            "\t}",
            "\tthrow Error('Unknown action: ' + action.type);",
            "}"
        ],
    },
Enter fullscreen mode Exit fullscreen mode

Note: I use a custom prefix to name my custom snippets (\\); this way I'm able to locate my snippets quicker in the suggestion box.

--

Cover image from unDraw

Thanks for reading.
šŸ’š

šŸ’– šŸ’Ŗ šŸ™… šŸš©
manuartero
Manuel Artero Anguita šŸŸØ

Posted on September 16, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related