[英]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.