小编典典

销毁嵌套对象:如何获得父级及其子级值?

reactjs

在下面的函数中,我获得带有属性的textarea对象current

此处,嵌套解构使用StartEnd变量。但是current变量不起作用。

function someFunction({ current: { selectionStart: Start, selectionEnd: End } }, AppStateSetter) {

    // do something with current, Start, and End
}

阅读 260

收藏
2020-07-22

共1个答案

小编典典

第一个解构仅创建StartEnd变量。如果要创建current为变量,则需要再次声明。

function ({ current: { selectionStart: Start, selectionEnd: End }, current }, AppStateSetter) {

// do something with current , Start , and End

}

您可以在Babel编译器上对其进行测试

这段代码:

const object = {
  current: {
    selectionStart: "prop 1",
    selectionEnd: "prop2"
  }
}

const { current: { selectionStart: Start, selectionEnd: End } } = object;

被转移到:

var object = {
  current: {
    selectionStart: "prop 1",
    selectionEnd: "prop2"
  }
};

var _object$current = object.current,
    Start = _object$current.selectionStart,
    End = _object$current.selectionEnd;

如您所见,current未创建变量。

2020-07-22