【问题标题】:Lodash 'get' not working when use in a Arrow Function (React)在箭头函数(React)中使用时,Lodash 'get' 不起作用
【发布时间】:2020-02-08 16:43:16
【问题描述】:

我在一个项目中使用Lodash,我正在尝试使用get 函数来过滤一些数据以激活按钮以将一些数据保存到端点,但是当我使用@987654324 @ 在 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】:

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

    导致:

    【讨论】:

    • 没错,我也可以删除 de {},它会起作用。谢谢。
    【解决方案2】:

    我的错误是没有返回 checkMandatory 验证器函数的值,解决它:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      • 2018-03-22
      • 2018-07-19
      • 2020-05-27
      相关资源
      最近更新 更多