大家好,我正在使用react-native
构建一个应用程序,因为我在身份验证时使用redux,我被困在某个地方,我得到了令牌作为随机对象,下面是我的代码
---减速器---
import { LOGIN_SUCCESS, LOGIN_FAIL, LOGOUT_SUCCESS } from '../actions/type';
import AsyncStorage from '@react-native-async-storage/async-storage';
const initialState = {
token: AsyncStorage.getItem('token'),
isAuthenticated: false,
isLoading: true
};
export default function (state = initialState, action) {
switch (action.type) {
case LOGIN_SUCCESS:
AsyncStorage.setItem('token', action.payload.token);
return {
...state,
...action.payload,
isAuthenticated: true,
isLoading: false,
};
case LOGIN_FAIL:
case LOGOUT_SUCCESS:
AsyncStorage.removeItem('token');
return {
...state,
...action.payload,
token: null,
isAuthenticated: false,
isLoading: false,
};
default:
return state;
}
}
---动作---
axios.post(api_url+'login', formdata, config)
.then((res) => {
console.log("res",res.data)
if(res.data.status == "true") {
dispatch({
type: LOGIN_SUCCESS,
payload: res.data.data,
});
dispatch(returnMsg(res.data.msg,res.data.status))
}
if(res.data.status == "false") {
dispatch({
type: LOGIN_FAIL,
payload: res.data,
});
dispatch(returnMsg(res.data.msg,res.data.status))
}
})
.catch((err) => {
console.log(err);
});
我正在调用应用程序中的reducer来检查以下令牌是代码
const actiondata = useSelector((state) => state.auth);
const {token} = actiondata;
console.log(token);
这是我控制台时得到的随机令牌值"{"_h":0,"_i":1,"_j":null,"_k":null}";
AsyncStorage.getItem是一个异步调用,需要等待。你要取回的对象是一个待定的承诺。