-
Notifications
You must be signed in to change notification settings - Fork 0
/
react.code-snippets
131 lines (131 loc) · 3.77 KB
/
react.code-snippets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
{
"Component.js": {
"scope": "javascript,javascriptreact",
"prefix": ["rfc", "Component"],
"description": "Create a React Functional Component",
"body": [
"import PropTypes from 'prop-types';",
"import React from 'react'",
"",
"export default function ${1:$TM_FILENAME_BASE}(props) {",
" return <div $2{...props} />;",
"}",
"",
"${1:$TM_FILENAME_BASE}.propTypes = {",
" children: PropTypes.node,",
"};",
"",
"${1:$TM_FILENAME_BASE}.defaultProps = {",
" children: undefined,",
"};",
""
]
},
"Component.ts": {
"scope": "typescript,typescriptreact",
"prefix": ["rfc", "Component"],
"description": "Create a React Functional Component",
"body": [
"import {HTMLAttributes} from 'react';",
"",
"type Props = HTMLAttributes<HTMLDivElement> & {};",
"",
"export default function ${1:$TM_FILENAME_BASE}(props: Props) {",
" return <div $2{...props} />;",
"}",
""
]
},
"RefComponent.js": {
"scope": "javascript,javascriptreact",
"prefix": ["rfcr", "RefComponent"],
"description": "Create a React functional component that forwards a reference.",
"body": [
"import PropTypes from 'prop-types';",
"import React, {forwardRef} from 'react';",
"",
"const ${1:$TM_FILENAME_BASE} = forwardRef(({",
" component: Element,",
" children,",
" ...props",
"}, ref) => {",
" return <Element ref={ref} $3{...props}>{children}</Element>;",
"});",
"",
"${1:$TM_FILENAME_BASE}.displayName = '${1:$TM_FILENAME_BASE}';",
"",
"export default ${1:$TM_FILENAME_BASE};",
"",
"${1:$TM_FILENAME_BASE}.propTypes = {",
" children: PropTypes.node.isRequired,",
" component: PropTypes.elementType,",
"};",
"",
"${1:$TM_FILENAME_BASE}.defaultProps = {",
" component: 'span',",
"};",
""
]
},
"RefComponent.ts": {
"scope": "typescript,typescriptreact",
"prefix": ["rfcr", "RefComponent"],
"description": "Create a React functional component that forwards a reference.",
"body": [
"import {HTMLAttributes, forwardRef} from 'react';",
"",
"type Props = HTMLAttributes<HTMLDivElement> & {};",
"",
"const ${1:$TM_FILENAME_BASE} = forwardRef<HTMLDivElement, Props>((props, ref) => {",
" return <div ref={ref} $2{...props} />;",
"});",
"",
"${1:$TM_FILENAME_BASE}.displayName = '${1:$TM_FILENAME_BASE}';",
"",
"export default ${1:$TM_FILENAME_BASE};",
""
]
},
"ExComponent.js": {
"scope": "javascript,javascriptreact",
"prefix": ["rfce", "ExComponent"],
"description": "Create an extendable React Functional Component",
"body": [
"import PropTypes from 'prop-types';",
"import React from 'react'",
"",
"export default function ${1:$TM_FILENAME_BASE}({",
" component: Element,",
" children,",
" ...props",
"}) {",
" return <Element $2{...props}>{children}</Element>;",
"}",
"",
"${1:$TM_FILENAME_BASE}.propTypes = {",
" children: PropTypes.node.isRequired,",
" component: PropTypes.elementType,",
"};",
"",
"${1:$TM_FILENAME_BASE}.defaultProps = {",
" component: 'span',",
"};",
""
]
},
"useReactHook": {
"scope": "javascript,javascriptreact,typescript,typescriptreact",
"prefix": ["rh", "ReactHook", "Hook"],
"description": "Create custom React Hook",
"body": [
"import {useCallback} from 'react';",
"",
"export default function ${1:$TM_FILENAME_BASE}() {",
" return useCallback(() => {",
" $1",
" }, []);",
"}",
""
]
}
}