简体   繁体   English

如何从箭头函数返回值?

[英]How to return a value from an arrow function?

I'm trying to return string from those two functions and attach it to my object parameters. 我试图从这两个函数返回字符串,并将其附加到我的对象参数。 Any idea how to make it return the string? 任何想法如何使它返回字符串?

 const getEmail = () => {
    ls.get('email').then((email) => {
        if (email == null) {
            return '';
        }
        return email;
    })
}

 const getPassword = () => {
    ls.get('password').then((password) => {
      if (password == null) {
            return '';
        }
        return password;
    })
}

const INITIAL_STATE = {
     email: getEmail(),
     password: getPassword(),
     user: null,
     error: '',
     loading: false
     };

You can't return a string from the future, but you can return the promise, store it on your state and then reuse it! 您不能从将来返回字符串,但是可以返回promise,将其存储在状态中,然后再使用它!

class Example extends Component {
  state = {
    email: '';
  }

  getEmail = () => {
    ls.get('email').then((email) => {
      if (email == null) {
        this.setState({ email: ''});
      }
      this.setState({ email });
    })
  }

  render()...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 箭头 function 预期返回值 - Arrow function expected a return value 预期在 reactjs 上的箭头函数末尾返回一个值如何修复此错误? - Expected to return a value at the end of arrow function on reactjs how to fix this error? 如何解决此“预期在箭头函数中返回值”错误 - How to fix this "expected to return a value in arrow function" error 如何在TypeScript中将箭头函数返回值分配给string [] - How to assign arrow function return value to string[] in TypeScript 如何避免Airbnb ESLint中的“箭头函数期望返回值”错误 - How to avoid “Arrow function expect to return a value” error in Airbnb ESLint 如何解决 eslint 中的“预期在箭头函数中返回值”错误 - How to solve "Expected to return a value in arrow function" error in eslint 如何使用 reactjs 修复“预期在箭头函数中返回值”? - How to fix "Expected to return a value in arrow function" with reactjs? 如何从 JS.then function 返回值 [使用箭头函数] - how to return values from JS .then function [using arrow function] 如何解决这个警告警告 Array.prototype.map() 期望从箭头函数数组回调返回的返回值? - How fix this warrning warning Array.prototype.map() expects a return value from arrow function array-callback-return? 如何从此箭头函数返回 pkgInfo 对象? - How do I return the pkgInfo object from this arrow function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM