Lodash'get'在箭头功能(反应)中使用时不起作用

问题描述 投票:2回答:1

我在项目中使用Lodash,并且尝试使用get功能来过滤一些数据以激活按钮以将一些数据保存到端点,但是当我使用get时在Arroe函数内部,它返回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);
}
javascript reactjs lodash
1个回答
3
投票

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'], []);

导致:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.