5

Does anybody knows what's the shortcut for React functional components snippet in Visual Studio? I have the ES7 React/Redux/GraphQL/React-Native snippets extension enabled in the editor.

Sergey Kirillov
  • 351
  • 1
  • 3
  • 10

6 Answers6

20

If you wish to create React functional component snippet in VS Code follow these steps.

  1. Go to File - Preferences - Configure User Snippets
  2. Dropdown will appear. Select New Global Snippets file and type <any-name> you want and hit enter
  3. File should be created with the following name <any-name>.code-snippet
  4. Paste this code. Anything you type in prefix will be your component trigger
{
  "React Functional Component": {
    "prefix": "rfc",
    "body": [
      "import React from \"react\"",
      "",
      "const ${1:name} = (props) => {",
      "  return (",
      "    <div>",
      "      $2",
      "    </div>",
      "  )",
      "};",
      "",
      "export default ${1:name};",
      ""
    ],
    "description": "React Functional Component"
  }
}
  1. Save the file
  2. Type (in this case) rfc and hit enter.
17

I used the rafce live template for new components.

This creates code like:

import React from 'react';

const MyComponent = () => {
    return (
        <div>
        
        </div>
    );
};

export default MyComponent;
Sergey Kirillov
  • 351
  • 1
  • 3
  • 10
4

I found the solution for my problem (tnx to Krzysztof Podmokły). I would like to share some improved code particularly for component in React (it fills name of the file instead of just name, and I deleted import React, as long as it doesn't needed anymore).

    {
      "React Functional Component": {
        "prefix": ["rfc"],
        "body": [
          "",
          "const $TM_FILENAME_BASE = () => {",
          "  return (",
          "    <div>",
          "      $2",
          "    </div>",
          "  )",
          "};",
          "",
          "export default $TM_FILENAME_BASE",
          ""
        ],
        "description": "React Functional Component"
      }
    }
Niko
  • 51
  • 2
2

If you want to generate function component snippet, use below key

Function component -> rnsf

Const component -> tsnf

VS code

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 01 '22 at 13:09
0

sfc is used with 'Simple React Snippets' extension to create statless functional components. The generated template is :

const ... = () => {
 return (  );
}

export default ...;
Faouzi
  • 929
  • 2
  • 15
  • 23
0

Based on the answer of @Niko, that provides the useful ${TM_FILENAME_BASE} placeholder, here is a snippet for Typescript functional component that, besides naming the component after the file, adds the interface for props as ComponentNameProps:

"React Functional Component": {
    "prefix": "rfc",
    "body": [
      "import React from 'react'",
      "",
      "export interface ${TM_FILENAME_BASE}Props {",
      "}",
      "export function $TM_FILENAME_BASE  ({}:${TM_FILENAME_BASE}Props)  {",
      "  return <></>",
      "};"
    ],
    "description": "React Functional Component"
  }

and, assuming that the file is named FileName.tsx, creates this:

import React from 'react'

export interface FileNameProps {
}
export function FileName  ({}:FileNameProps)  {
  return <></>
};
Porkopek
  • 895
  • 9
  • 20