1、使用过ESLint
插件的都知道,在Vscode
中,如果你取值不用解构的方式,它就会建议你使用解构赋值。
2、在项目中,我最常用的就是对象解构
的最常用方式。
3、这种写法非常基本,所有的取值都可以这样写。就这样,最简单的写出了符合react官方建议的写法和符合ESLint
规范。
从this.state、this.props中取值
const listType = this.state.listType; // 一般写法
const {
listType,fileList,downloadTitle } = this.state;
const {
dispatch, registrationId } = this.props;
4、
解构动态属性
在对象解构中使用变量
,可方便取值赋值操作。
在 []中写变量值
const {
[listType]: {
infoList } } = this.props;
应用场景也很简单能想到,
- 你可以根据情况的控制变量,控制你的
infoList
从哪里去取值,可以实现更复杂的场景。
在dispatch
中同样可以这样使用
const {
keyword, typeId, state, listType } = this.state;
dispatch({
type: `${
listType}/getApplicationPageList`,
payload: {
.......
6、才疏学浅,简单分享。如有不当的地方请指正。