簡體   English   中英

在箭頭 Function 中使用時,Lodash 'get' 不起作用(反應)

[英]Lodash 'get' not working when use in a Arrow Function (React)

我在一個項目中使用Lodash ,我正在嘗試使用get function 來過濾一些數據以激活按鈕以將一些數據保存到端點,但是當我使用 Arroe Function 內部的get時,它返回undefined ,然后我在變量分配中使用相同的代碼可以正常工作,您可以在此StackBlitz中檢查錯誤。 其他人得到這種錯誤? 提前致謝。

componentDidMount() {
  const checkMandatory = tab => {
    get(this.state.data, [tab, 'data', 'datosCalculados'], []);
  };
  const obligatorios = checkMandatory('ingresos');
  const obligatorios2 = get(this.state.data, ['ingresos', 'data', 'datosCalculados'], []);
  console.log('Obligatorios: ', obligatorios);
  console.log('Obligatorios2: ', obligatorios2);
}

return不見了

代替

const checkMandatory = tab => {
  get(this.state.data, [tab, 'data', 'datosCalculados'], []);
};

const checkMandatory = tab => { 
  return get(this.state.data, [tab, 'data', 'datosCalculados'], []); 
};

或者更好:

const checkMandatory = tab => get(this.state.data, [tab, 'data', 'datosCalculados'], []);

導致:

在此處輸入圖像描述

我的錯誤沒有返回checkMandatory驗證器 function 上的值,以解決它:

const checkMandatory = tab => get(this.state.data, [tab, 'data', 'datosCalculados'], []);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM