36
loading...
This website collects cookies to deliver better user experience
// MyComponent.tsx
interface MyComponentProps {}
export function MyComponent(props: MyComponentProps) {
return (
// ... JSX Code here
);
}
MyComponent
4 times.forEach
and you start typing “for” on a .js
file, VSCode suggests:foreach
:array
field, so you can type the name of the array variable you want to iterate through. When you press Tab
the cursor moves to the next position, which is element
. You can then define the name of the item reference in each iteration. Press Tab
again, and you end up in the body of the function.const myArray = [1, 2, 3, 4, 5];
myArray.forEach(item => {
// code goes here
});
foreach
snippet by typing “for”.// new-component-react.code-snippets
{
"Print to console": {
"scope": "javascript, typescript",
"prefix": "log",
"body": [
"console.log('$1');",
"$2"
],
"description": "Log output to console"
}
}
log
in a .js
or .ts
file, you can see the snippet shows up as a suggestion:log
will show the following:description
is a more detailed description that shows on the side of the option. The prefix
field is what keyword is going to be used to find the snippet in the auto suggestion sub menu. The scope
defines in what file types typing the snippet will show on auto suggestion (in this first example, .js
or .ts
files).body
field is the template code that will be provided for you to complete. It’s an array where each line in the array is a line on the file. To create the body of our snippet, we need to understand how to add placeholders.$
sign. The number after it is the Tab
order. You can also provide a default text if you like. For example, in the foreach
snippet the first placeholder has a default text of array
and the second has element
as default text. This can be achieved by placing the default text after the placeholder and enclosing it with curly braces, like this:{$0:array} // will be a placeholder with 'array' as default text
{$1:element} // will be a placeholder with 'element' as default text
react-component-ts
. Here is the snippet:{
"React component with typescript": {
"scope": "typescriptreact",
"prefix": "react-component-ts",
"body": [
"interface $1Props {}\n",
"export function $1(props: $1Props) {",
"\treturn ($2);",
"}\n",
"description": "Basic react component with typescript"
}
}
typescriptreact
which basically is the way of telling VSCode that the file format where the snippet may be available is .tsx
.$1
on multiple places, when we use the snippet and start typing the name of the component, we will also have the name of the interface and props type already written. After that, if we press Tab
, the cursor goes inside the return parenthesis and you can start coding your component!$TM_FILENAME_BASE
returns the filename to be used in the snippet. So, the final version would look like this:{
"React component with typescript": {
"scope": "typescriptreact",
"prefix": "react-component-ts",
"body": [
"interface ${TM_FILENAME_BASE}Props {}\n",
"export function $TM_FILENAME_BASE(props: ${TM_FILENAME_BASE}Props) {",
"\treturn ($1);",
"}\n",
"description": "Basic react component with typescript"
}
}
To name the interface we have to use the placeholder in a slightly different way (${TM_FILENAME_BASE}
), so VSCode understands that the variable name is TM_FILENAME_BASE
and not TM_FILEBASE_NAMEProps
(which wouldn’t work).
.tsx
component and inside it search for the preffix: