javascript - 为同一个对象属性多次解构
问题描述
有时我会进行嵌套破坏,这意味着不止一个级别,这可能很危险,但我只确保该属性存在,否则我将拥有undefined error的属性。
我最近做了这个,
const {
match: {
params: { id: UserId }
},
match
} = this.props
我怀疑是否是好的代码。我需要match.params.id
,我也需要match
对象,所以这个重复的“变量”可以吗?
解决方案
不这样做:
const {match} = this.props;
const {params: {id: userId} = {} } = match || {};
推荐阅读
- cmake - CMake:仅针对特定目标从 CMAKE_C_FLAGS_RELEASE 中删除编译标志
- python - NameError: name 'Pick' is not defined
- r - 如何在R中的数据表列表中添加总计和百分比行?
- elasticsearch - 在 Kibana 中获取 X-Forwarded-For 真实 IP
- isabelle - Isabelle 是否弃用了假设放电的规则方法?
- c# - .Net Core 2.2 更新到 .Net Core 3.1 由于缺少程序集而导致 HTTP 错误 500.31
- javascript - 在 vue.js 组件中声明 typescript 接口道具
- json - 拆分两个字符串并配对对应的部分
- julia - 为什么 julia 需要很长时间才能导入包?
- node.js - 如何使用 JWT 为我的 Google Cloud Functions 实现 POST 请求