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.
6 Answers
If you wish to create React functional component
snippet in VS Code
follow these steps.
- Go to
File - Preferences - Configure User Snippets
- Dropdown will appear. Select
New Global Snippets file
and type<any-name>
you want and hit enter - File should be created with the following name
<any-name>.code-snippet
- 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"
}
}
- Save the file
- Type (in this case)
rfc
and hit enter.

- 494
- 6
- 12
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;

- 351
- 1
- 3
- 10
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"
}
}

- 51
- 2
If you want to generate function component snippet, use below key
Function component -> rnsf
Const component -> tsnf
VS code

- 29
- 2
-
1As 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
sfc
is used with 'Simple React Snippets' extension to create statless functional components. The generated template is :
const ... = () => {
return ( );
}
export default ...;

- 929
- 2
- 15
- 23
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 <></>
};

- 895
- 9
- 20