Common React Snippets for VS Code.
These snippets were selected carefully from my own day-to-day React use. Not everything in React is included here. This is a hand selected set of snippets that work the way that you would expect, not just a copy of the documentation.
Snippet | Renders |
---|---|
imr |
Import React |
imrc |
Import React / Component |
imrs |
Import React / useState |
imrse |
Import React / useState useEffect |
impc |
Import React / PureComponent |
imfc |
Import React / FunctionComponent |
cc |
Class Component |
pc |
Pure Component |
ccc |
Class Component With Constructor |
fc |
Function Component With hooks |
sfc |
Stateless Function Component |
cdm |
componentDidMount |
uef |
useEffect Hook |
gds |
getDerivedStateFromProps |
scu |
shouldComponentUpdate |
cdu |
componentDidUpdate |
cwum |
componentWillUnmount |
cdc |
componentDidCatch |
gsbu |
getSnapshotBeforeUpdate |
ss |
setState |
ssf |
Functional setState |
usf |
Declare a new state variable using State Hook |
ren |
render |
rprop |
Render Prop |
ima |
Import antd |
imp |
Import module |
imc |
Import classnames |
ims |
Import style |
imms |
Import module style |
mobs |
Mobx observable with comment |
mact |
Mobx action with comment |
dwc |
Div with module class |
dwgc |
Div with global class |
mlg |
My Log |
import React from 'react'
import React, { Component } from 'react'
import React, { useState } from 'react'
import React, { useState, useEffect } from 'react'
import PropTypes from 'prop-types'
import React, { PureComponent } from 'react'
import React, { Component } from 'react'
interface |Props {
}
interface |State {
}
class | extends Component<|Props, |State> {
state = { | },
render() {
return ( | )
}
}
export default |
import React, { PureComponent } from 'react'
interface |Props {
}
interface |State {
}
class | extends PureComponent<|Props, |State> {
state = { | },
render() {
return ( | )
}
}
export default |
import React, { Component } from 'react'
interface |Props {
}
interface |State {
}
class | extends Component<|Props, |State> {
constructor(props: |Props) {
super(props)
this.state = { | }
}
render() {
return ( | )
}
}
export default |
import React, { useState, useEffect } from 'react'
interface |Props {
}
function | (props: |Props) {
const { } = props
const [|, set|] = useState(|)
useEffect(() => {
}, [])
return (
<div></div>
)
}
export default |
import React from 'react'
interface |Props {
}
function | (props: |Props) {
const { } = props
return (
<div></div>
)
}
export default |
componentDidMount() {
|
}
useEffect(() => {
|
}, [])
static getDerivedStateFromProps(nextProps, prevState) {
|
}
shouldComponentUpdate(nextProps, nextState) {
|
}
componentDidUpdate(prevProps, prevState) {
|
}
componentWillUnmount() {
|
}
componentDidCatch(error, info) {
|
}
getSnapshotBeforeUpdate(prevProps, prevState) {
|
}
this.setState({ | : | })
this.setState(prevState => {
return { | : prevState.| }
})
const [|, set|] = useState()
Hit Tab to apply CamelCase to function. e.g. [count, setCount]*
render() {
return (
|
)
}
class | extends Component {
state = { | },
render() {
return this.props.render({
|: this.state.|
})
}
}
export default |
import { | } from 'antd'
import cls from 'classnames'
import styles from './|.less'
import styles from './|.module.less'
/** */
@observable | = |
/** */
@action | = async () => {
|
}
<div className={styles.|}>|</div>
<div className='|'>|</div>
console.log('========start========')
console.log(|)
console.log('=========end=========')
todo...