私はreduxには新しく、認証の使用例の簡単な例を解明しようとしています。このコードのWebバージョンは、localstorageから初期状態を取得し、localstorageにも設定します。この例をreact-nativeに変換すると、localstorageが非同期のAsyncStorageに変更され、約束が返されます。減速機で非同期の約束を扱う方法
減速機で非同期初期化子を処理するにはどうすればよいですか?
import { AsyncStorage } from 'react-native';
import {
LOGIN_REQUEST, LOGIN_SUCCESS, LOGIN_FAILURE, LOGOUT_SUCCESS,
} from '../actions/login';
const initialState = {
isFetching: false,
token: null, // AsyncStorage.getItem('token'),
profile: null, // AsyncStorage.getItem('profile'),
isLoggedIn: false,
errorMessage: null,
};
// The auth reducer. The starting state sets authentication
// based on a token in asyncstorage. In addition we would also null
// the token if we check that it's expired
export default (state = initialState, action) => {
switch (action.type) {
case LOGIN_REQUEST:
return Object.assign({}, state, {
isFetching: true,
isLoggedIn: false,
token: null,
user: null,
errorMessage: '',
});
case LOGIN_SUCCESS:
// todo set the async
// AsyncStorage.multiSet(['token', 'profile'], [action.token, action.profile])
return Object.assign({}, state, {
isFetching: false,
isLoggedIn: true,
token: action.token,
user: action.profile,
errorMessage: '',
});
case LOGIN_FAILURE:
return Object.assign({}, state, {
isFetching: false,
isLoggedIn: false,
token: null,
errorMessage: action.message,
});
case LOGOUT_SUCCESS:
// AsyncStorage.multiRemove(['token', 'profile'])
return Object.assign({}, state, {
isFetching: true,
isLoggedIn: false,
token: null,
});
default:
return state;
}
};