This extension contains code snippets for React with Typescript and Generate React Component.
- TypeScript (.ts)
- TypeScript React (.tsx)
Trigger | Content |
---|---|
isc | import styled-components |
sc | define a styled component |
scc | extends a styled component |
scp | Use props inside styled-component |
icm | import a css module |
ss | use css module |
icn | import classnames |
import styled from 'styled-components';
const $1 = styled.$2`
$3
`;
const $1 = styled($2)`
$3
`;
${props => props.$1};
import styles from './index.less';
className = { styles };
import classNames from 'classnames';
Trigger | Content |
---|---|
ust | React useState() hook |
uef | React useEffect() hook |
urf | React useRef() hook |
sl | define a selector |
usl | use selector |
udp | useDispatch |
rcc | define a class component |
rfc | define a function component |
cwm | componentWillMount |
cdm | componentDidMount |
cwr | componentWillReceiveProps |
scu | shouldComponentUpdate |
cwup | componentWillUpdate |
cdup | componentDidUpdate |
cwun | componentWillUnmount |
sst | setState use object |
ssf | setState use function |
slice | Create a slice by @reduxjs/toolkit |
mobx | Create a Mobx Store |
const [$1, set$1] = useState($2);
useEffect(() => {
$1;
});
const $1 = useRef($2);
export const select$2 = (state: RootState) => state$1;
const $1 = useSelector((state: RootState) => state.$1);
const dispatch = useDispatch();
import React, { PureComponent } from 'react';
type DemoProps = {};
type DemoState = Readonly<{}>;
class Demo extends PureComponent<DemoProps, DemoState> {
readonly state: DemoState = {};
render() {
return <div>I am Demo</div>;
}
}
export default Demo;
import React, { FC } from 'react';
type DemoProps = {};
const Demo: FC<DemoProps> = (props) => {
return <div>I am Demo</div>;
};
export default Demo;
componentWillMount() {
$1
}
componentDidMount() {
$1
}
componentWillReceiveProps(nextProps: Props) {
$1
}
shouldComponentUpdate(nextProps: Props, nextState: State) {
$1
}
componentWillUpdate(nextProps: Props, nextState: State) {
$1
}
componentWillUnmount() {
$1
}
this.setState({ $1 });
this.setState((state, props) => {
return { $1 };
});
import { createSlice, PayloadAction, createAsyncThunk } from '@reduxjs/toolkit';
import { RootState } from './rootReducer';
export const initialState = {
value: 0,
};
export const changeCountAsync = createAsyncThunk('counter/changeCountAsync', async (count: number) => {
await new Promise((resolve) => {
setTimeout(resolve, 3000);
});
return count;
});
const counter = createSlice({
name: 'counter',
initialState,
reducers: {
changeCount(state, action: PayloadAction<number>) {
switch (true) {
case action.payload > 0:
state.value++;
break;
case action.payload < 0:
state.value--;
break;
default:
}
},
},
extraReducers: (builder) => {
builder.addCase(changeCountAsync.fulfilled, (state, action) => {
state.value += action.payload;
});
},
});
export const { changeCount } = counter.actions;
export const selectCount = (state: RootState) => state.counter.value;
export default counter.reducer;
import { makeAutoObservable } from 'mobx';
class Counter {
constructor() {
makeAutoObservable(this);
}
count = 1;
increment() {
this.count += 1;
}
decrement() {
this.count -= 1;
}
}
export default Counter;
Trigger | Content |
---|---|
req | Require a package to const |
mde | Module exports from Common JS, node syntax at ES6 |
sti | setInterval |
sto | setTimeout |
prom | Creates and returns a new Promise in the standard ES6 syntax |
thenc | Add the .then and .catch methods to handle promises |
cer | console.error() |
clg | console.log() |
cwa | console.warn() |
cin | console.info() |
cti | console.time() |
cte | console.timeEnd() |
const packageName = require('packageName');
module.exports = {
$1,
};
setInterval(() => {
$1;
}, intervalInms);
setTimeout(() => {
$1;
}, delayInms);
return new Promise((resolve, reject) => {
$1;
});
.then((result) => {
$1
}).catch((err) => {
$2
});
console.error(object);
console.log(object);
console.warn(object);
console.info(object);
console.time(object);
console.timeEnd(object);