首页 > 解决方案 > 为同一个对象属性多次解构

问题描述

有时我会进行嵌套破坏,这意味着不止一个级别,这可能很危险,但我只确保该属性存在,否则我将拥有undefined error的属性。

我最近做了这个,

const {
        match: {
          params: { id: UserId }
        },
        match
      } = this.props 

我怀疑是否是好的代码。我需要match.params.id,我也需要match对象,所以这个重复的“变量”可以吗?

标签: javascriptreactjsecmascript-6

解决方案


不这样做:

const {match} = this.props;
const {params: {id: userId} = {} } = match || {};

推荐阅读