A collection of common Javascript and Typescript vim snippets for developing React applications. The snippets within this repo rely on UltiSnips as the snippet provider.
I recommend using a package manager such as vim-plug to install your vim packages.
call plug#begin('~/.vim/plugged')
Plug 'SirVer/ultisnips'
Plug 'mlaursen/vim-react-snippets'
call plug#end()You can always see my full .vimrc for my Javascript/Typescript configuration and setup.
- Cheatsheet
- PropTypes
- Class Components (Javascript)
- Function Components (Javascript)
- React Lifecycle (Javascript)
- Hooks and Effects (Javascript)
- General Redux (Javascript)
- Redux Toolkit (Javascript)
- Class Components (Typescript)
- Function Components (Typescript)
- React Lifecycle (Typescript)
- React Event Types (Typescript Only)
- General Redux (Typescript)
- Redux Toolkit (Typescript)
- Hooks and Effects (Typescript)
- Importing
- Exporting
- Logging
- NODE_ENV
- Tests (Javascript and Typescript)
I will list the current supported snippets below and their implementation. I
recommend checking out the full source files in the UltiSnips folder to see
the full tabstop locations. The examples below will use $TABSTOP or $NAME to
indicate that there is a tabstop or a tabbable/replaceable variable. Finally, if
you see $CFN or $CFN_, it will be the Current File Name (the trailing
underscore will not be included, it is just added to show separation when
something else follows it).
| Shortcut | Expands To |
|---|---|
pt.a |
React.PropTypes.array |
pt.ar |
React.PropTypes.array.isRequired |
pt.b |
React.PropTypes.bool |
pt.br |
React.PropTypes.bool.isRequired |
pt.f |
React.PropTypes.func |
pt.fr |
React.PropTypes.func.isRequired |
pt.nu |
React.PropTypes.number |
pt.nur |
React.PropTypes.number.isRequired |
pt.o |
React.PropTypes.object |
pt.or |
React.PropTypes.object.isRequired |
pt.s |
React.PropTypes.string |
pt.sr |
React.PropTypes.string.isRequired |
pt.no |
React.PropTypes.node |
pt.nor |
React.PropTypes.node.isRequired |
pt.e |
React.PropTypes.element |
pt.er |
React.PropTypes.element.isRequired |
pt.ao |
React.PropTypes.arrayOf |
pt.aor |
React.PropTypes.arrayOf.isRequired |
pt.io |
React.PropTypes.instanceOf |
pt.ior |
React.PropTypes.instanceOf.isRequired |
pt.oo |
React.PropTypes.objectOf |
pt.oor |
React.PropTypes.objectOf.isRequired |
pt.sh |
React.PropTypes.shape |
pt.shr |
React.PropTypes.shape.isRequired |
rce ->
import React, { Component } from "react"
export default class $CFN extends Component {
constuctor(props) {
super(props)
this.state = {}
}
render() {
return null
}
}rcc ->
class $CFN extends Component {
render() {
return null
}
}rcon ->
constructor(props) {
super(props)
this.state = {}
}spt ->
static propTypes = {
$TABSTOP
}sdp ->
static defaultProps = {
$TABSTOP
}rcf ->
$TABSTOP = ($TABSTOP) => {
$TABSTOP
}fce ->
import React from "react"
const $CFN = (props) => {
return null
}
export default $CFNsfce ->
import React from "react"
const $CFN = () => {
return null
}
export default $CFNffce ->
import React, { forwardRef } from "react"
const $CFN = forwardRef(function $CFN(props, ref) {
return <div ref={ref}></div>
})
export default $CFNcpt ->
$CFN.propTypes = {
$TABSTOP,
}cdp ->
$CFN.defaultProps = {
$TABSTOP,
}gds ->
static getDerivedStateFromProps(nextProps, prevState) {
return null
}gde ->
static getDerivedStateFromError(error) {
return null
}cdm ->
componentDidMount() {
$TABSTOP
}scu ->
shouldComponentUpdate(nextProps, nextState) {
$TABSTOP
}gsbu ->
getSnapshotBeforeUpdate(prevProps, prevState) {
$TABSTOP
}cdu ->
componentDidUpdate(prevProps, prevState, $SNAPSHOT) {
$TABSTOP
}cdc ->
componentDidCatch(error, info) {
$TABSTOP
}cwum ->
componentWillUnmount() {
$TABSTOP
}useS ->
const [$STATE, set$STATE] = useState($TABSTOP)useE ->
useEffect(() => {
$TABSTOP
}, [$TABSTOP])useEA ->
useEffect(() => {
let cancelled = false
;(async function $DOWORK() {
// async work here
$TABSTOP
if (cancelled) {
return
}
$TABSTOP
})()
return () => {
cancelled = true
}
}, [$TABSTOP])useC ->
const context = useContext($TABSTOP)or inline:
return useC ->
return useContext($TABSTOP)useRed ->
const [$STATE, $DISPATCH] = useReducer($REDUCER, $NULL)useCB ->
const $CALLBACK = useCallback(($TABSTOP) => {
$TABSTOP
}, [$TABSTOP])useM ->
const $MEMOIZED = useMemo(() => {
$TABSTOP
}, [$TABSTOP])useR ->
const $REF = useRef($TABSTOP)useI ->
useImperitiveHandle($REF, () => ({
$TABSTOP,
}), [$TABSTOP])useL ->
useLayoutEffect(() => {
$TABSTOP
}, [$TABSTOP])useDV ->
useDebugValue($NULL)mc ->
const $THING = "$THING"useD ->
const dispatch = useDispatch()or inline:
const dispatch = useD ->
const dispatch = useDispatch()useSL ->
const $VALUE = useSelector(($STATE) => $SELECTOR)or inline:
const checked = useSL ->
const checked = useSelector(($STATE) => $SELECTOR)cs ->
const { actions, reducer } = createSlice({
name: "$CFN",
initialState: $TABSTOP,
reducers: {
$TABSTOP,
},
})esc ->
import { createSlice } from "@reduxjs/toolkit"
const { actions, reducer } = createSlice({
name: "$CFN",
initialState: $TABSTOP,
reducers: {
$TABSTOP,
},
})
export const { $TABSTOP } = actions
export default reducercpr ->
$TABSTOP: {
reducer(state, action) {
$$TABSTOP
},
prepare($TABSTOP) {
return { payload: { $TABSTOP } }
}
}cat ->
export const $TABSTOP = createAsyncThunk("$TABSTOP", async ($TABSTOP) => {
$TABSTOP
})or inline:
export const doThing = cat ->
export const doThing = createAsyncThunk("$TABSTOP", async ($TABSTOP) => {
$TABSTOP
})rce ->
import React, { Component } from "react"
export default class $CFN extends Component {
public render() {
return null
}
}rcep ->
import React, { Component } from "react"
export interface $CFN_Props {}
export default class $CFN extends Component<$CFN_Props> {
public render() {
return null
}
}rceps ->
import React, { Component } from "react"
export interface $CFN_Props {}
export interface $CFN_State {}
export default class $CFN extends Component<$CFN_Props, $CFN_State> {
constructor(props: $CFN_Props) {
super(props)
this.state = {}
}
public render() {
return null
}
}rcc ->
class $CFN extends Component {
public render() {
return null
}
}rcon ->
constructor(props: $CFN_Props) {
super(props)
this.state = {}
}spt ->
public static propTypes = {
$TABSTOP
}sdp ->
public static defaultProps = {
$TABSTOP
}sdpt ->
public static defaultProps: DefaultProps = {
$TABSTOP
}rcf ->
$TABSTOP = ($TABSTOP) => {
$TABSTOP
}Note: These are different than the Javascript versions on purpose and use the
function syntax instead of a "const + arrow function".
fce ->
import React, { ReactElement } from "react"
export interface $CFN_Props {}
export default function $CFN(props: $CFN_Props): ReactElement | null {
return null
}sfce ->
import React, { ReactElement } from "react"
export default function $CFN(): ReactElement | null {
return null
}ffce ->
import React, { forwardRef } from "react"
export interface $CFNProps {
$TABSTOP
}
export default forwardRef<HTML$TABSTOPElement, $CFN_Props>(function $CFN(
props,
ref
) {
return <div ref={ref}></div>
})cpt ->
$CFN.propTypes = {
$TABSTOP,
}cdp ->
$CFN.defaultProps = {
$TABSTOP,
}cdpt ->
const defaultProps: DefaultProps = {
$TABSTOP,
}
$CFN.defaultProps = defaultPropsgds ->
static getDerivedStateFromProps(nextProps: $CFN_Props, prevState: $CFN_State) {
return null
}gde ->
static getDerivedStateFromError(error: Error) {
return null
}cdm ->
componentDidMount() {
$TABSTOP
}scu ->
shouldComponentUpdate(nextProps: $CFN_Props, nextState: $CFN_State) {
$TABSTOP
}gsbu ->
getSnapshotBeforeUpdate(prevProps: $CFN_Props, prevState: $CFN_State) {
$TABSTOP
}cdu ->
componentDidUpdate(prevProps: $CFN_Props, prevState: $CFN_State, $SNAPSHOT) {
$TABSTOP
}cdc ->
componentDidCatch(error: Error, info: ErrorInfo) {
$TABSTOP
}cwum ->
componentWillUnmount() {
$TABSTOP
}| Shortcut | Expands to |
|---|---|
me |
event: MouseEvent<HTMLButtonElement> |
te |
event: TouchEvent<HTMLButtonElement> |
ke |
event: KeyboardEvent<HTMLInputElement> |
che |
event: ChangeEvent<HTMLInputElement> |
fe |
event: FocusEvent<HTMLElement> |
foe |
event: FormEvent<HTMLInputElement> |
meh |
MouseEventHandler<HTMLButtonElement> |
teh |
TouchEventHandler<HTMLButtonElement> |
keh |
KeyboardEventHandler<HTMLInputElement> |
cheh |
ChangeEventHandler<HTMLInputElement> |
feh |
FocusEventHandler<HTMLInputElement> |
foeh |
FormEventHandler<HTMLElement> |
Note: The
event:andButton/Inputparts are a tabstop which can be removed or changed.
mc ->
const $THING = "$THING"useDS ->
const dispatch: $AppDispatch = useDispatch()useD ->
const dispatch = useAppDispatch()or inline:
const dispatch = useD ->
const dispatch = useAppDispatch()useSL ->
const $VALUE = useSelector(($STATE: AppState) => $SELECTOR)or inline:
const checked = useSL ->
const checked = useSelector(($STATE: AppState) => $SELECTOR)useAS ->
const $VALUE = useAppSelector(($STATE) => $SELECTOR)or inline:
const checked = useAS ->
const checked = useAppSelector(($STATE: AppState) => $SELECTOR)cs ->
const { actions, reducer } = createSlice({
name: "$CFN",
initialState: $TABSTOP,
reducers: {
$TABSTOP,
},
})esc ->
import { createSlice } from "@reduxjs/toolkit"
const { actions, reducer } = createSlice({
name: "$CFN",
initialState: $TABSTOP,
reducers: {
$TABSTOP,
},
})
export const { $TABSTOP } = actions
export default reducercpr ->
$TABSTOP: {
reducer(state, action: $PayloadAction<$TABSTOP>) {
$$TABSTOP
},
prepare($TABSTOP) {
return { payload: { $TABSTOP } }
}
}cat ->
export const $TABSTOP = createAsyncThunk("$TABSTOP", async ($TABSTOP) => {
$TABSTOP
})or inline:
export const doThing = cat ->
export const doThing = createAsyncThunk("$TABSTOP", async ($TABSTOP) => {
$TABSTOP
})useS ->
const [$STATE, set$STATE] = useState$TABSTOP($TABSTOP)useE ->
useEffect(() => {
$TABSTOP
}, [$TABSTOP])useEA ->
useEffect(() => {
let cancelled = false
;(async function $DOWORK(): Promise<$VOID> {
// async work here
$TABSTOP
if (cancelled) {
return
}
$TABSTOP
})()
return () => {
cancelled = true
}
}, [$TABSTOP])useC ->
const context = useContext$TABSTOP($TABSTOP)or inline:
return useC ->
return useContext$TABSTOP($TABSTOP)useRed ->
const [$STATE, $DISPATCH] = useReducer<typeof $REDUCER>($REDUCER, $NULL)useRedUT ->
const [$STATE, $DISPATCH] = useReducer($REDUCER, $NULL)useCB ->
const $CALLBACK = useCallback(($TABSTOP) => {
$TABSTOP
}, [$TABSTOP])useM ->
const $MEMOIZED = useMemo(() => {
$TABSTOP
}, [$TABSTOP])useR ->
const $REF = useRef$TABSTOP(TABSTOP)useI ->
useImperitiveHandle($REF, () => ({
$TABSTOP,
}), [$TABSTOP])useL ->
useLayoutEffect(() => {
$TABSTOP
}, [$TABSTOP])useDV ->
useDebugValue($NULL)| Shortcut | Expands to |
|---|---|
rc |
const packageName = require('package-name') |
imp |
import packageName from 'package-name' |
impf |
import File from './File' |
impn |
import { nested } from 'package-or/path' |
impa |
import * as Thing from 'package-or/path' |
impp |
import './file' |
icn |
import cn from 'classnames' |
ism |
import styles from './$CFN.module.scss' |
| Shortcut | Expands to |
|---|---|
exp |
export { default } from './CurrentFolder' |
expf |
export File from './File' |
expn |
export { nested } from 'package-or/path |
expa |
export * from 'package-or/path' |
expd |
export { default as Thing } from './Thing' |
| Shortcut | Expands to |
|---|---|
cl |
console.log($TABSTOP) |
clv |
console.log('variable: ', variable) |
ce |
console.error($TABSTOP) |
cev |
console.error('variable: ', $TABSTOP) |
cw |
console.warrn($TABSTOP) |
ct |
console.table($TABSTOP) |
cd |
console.debug($TABSTOP) |
| Shortcut | Expands to |
|---|---|
dev |
process.env.NODE_ENV !== "production" |
prod |
process.env.NODE_ENV === "production" |
rtf ->
import React from "react"
import { render } from "@testing-library/react"
import $CFN from "../$CFN"
describe("$CFN", () => {
it("should $TABSTOP", () => {
$TABSTOP
})
})rhtf ->
import React from "react"
import { renderHook } from "@testing-library/react-hooks"
import $TABSTOP from "../$CFN"
describe("$CFN", () => {
it("should $TABSTOP", () => {
$TABSTOP
})
})desc ->
describe('$CFN', () => {
it('should $TABSTOP', () => {
$TABSTOP
)}
})it ->
it("should $TABSTOP", () => {
$TABSTOP
})ait ->
it("should $TABSTOP", async () => {
$TABSTOP
})todo ->
it.todo("should $TABSTOP")es ->
expect($TABSTOP_container).toMatchSnapshot()

