如何使用从 AsyncStorage 接收到的值进行 Base64 编码?
我将值从 AsyncStorage 设置为 App 组件的状态(我在 thunk 中调用 AsyncStorage.setItem(),它工作正常:将值分配给存储)
class App extends React.Component {
state = {
userName: "",
userPassword: ""
}
getLogin = async () => {
try {
const value = await AsyncStorage.getItem("phoneNumber") || 'none'
if(value !== null) {
console.log(value , "getLogin");
this.setState({userName: value}, console.log(this.state.userName, 'getLogin'))
}
} catch(e) {
console.log(e)
}
}
getPassword = async () => {
try {
const value = await AsyncStorage.getItem("verificationCode") || 'none'
if(value !== null) {
console.log(value, "getPassword");
this.setState({userPassword: value}, console.log(this.state.userPassword, 'getPassword'))
}
} catch(e) {
console.log(e)
}
}
componentDidMount(){
this.getLogin()
this.getPassword()
}
在 render() 应用程序中,我传递了函数的状态
使成为() {
const {userName, userPassword} = this.state
getServicesApi(userName, userPassword)
}
接下来,我使用getServicesApi函数来传递参数
导出函数getServicesApi(用户名,用户密码){
const base64token = () => {
if(userName && userPassword) return Base64.btoa(`+${userName}:${userPassword}`)
}
console.log(base64token(), "base64token")
const headers = () => new Headers({
"Content-Type" : "application/json",
"Authorization" : `Basic ${base64token()}`
})
}
结果,控制台输出(未定义,未定义,“base64token”)
我在哪里做错了?大概 componentDidMount() 不起作用。有处理这种情况的模式吗?