17

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.

8 Answers 8

43

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.
Sign up to request clarification or add additional context in comments.

3 Comments

It may be worth adding a scope line, `"scope": "javascript, typescript"
in latest versions of react, its not necessary to import react. But just mentioning, understand that the main motive is to show how to make a snippet.
Thanks for this, I didn't know it was that easy to create snippets
37

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;

Comments

9

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"
      }
    }

Comments

4

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

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

export default ...;

Comments

4

Intstall this extension (is required for creating these components) https://marketplace.visualstudio.com/items?itemName=dsznajder.es7-react-js-snippets

Then you can create file, ex: MyComponent.jsx (or .tsx) Type in rafce (VS Code will suggest you some of these) and hit Enter

1 Comment

I tried, not working
1

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

Function component -> rnsf

Const component -> tsnf

VS code

1 Comment

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.
1

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 <></>
};

Comments

1

Those who are looking to use this as react native,

They can add the blow code to javascript.json file using above instructions.

  {
  "React Native Functional Component": {
    "prefix": "rafce",
    "body": [
      "import React from 'react';",
      "import { View, Text, StyleSheet } from 'react-native';",
      "",
      "const ${TM_FILENAME_BASE} = () => {",
      "  return (",
      "    <View style={styles.container}>",
      "      <Text>${TM_FILENAME_BASE}</Text>",
      "    </View>",
      "  );",
      "};",
      "",
      "const styles = StyleSheet.create({",
      "  container: {",
      "    flex: 1,",
      "    justifyContent: 'center',",
      "    alignItems: 'center'",
      "  }",
      "});",
      "",
      "export default ${TM_FILENAME_BASE};"
    ],
    "description": "React Native Functional Component with StyleSheet"
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.