Javascript 如何在redux操作中返回承诺-react native

Javascript 如何在redux操作中返回承诺-react native,javascript,reactjs,typescript,react-native,promise,Javascript,Reactjs,Typescript,React Native,Promise,我正在尝试在需要时从redux获取位置。在redux将位置保存到状态后,我需要返回一个承诺,因为我需要在屏幕中显示该数据 这里是我的行动,减缩,存储,以及我如何使用它们 LocationRedux.tsx import * as Type from './LocationReduxTypes'; const initialState: Type.LocationState = { location: undefined, }; const LocationRedux = ( st

我正在尝试在需要时从redux获取位置。在redux将位置保存到状态后,我需要返回一个承诺,因为我需要在屏幕中显示该数据

这里是我的行动,减缩,存储,以及我如何使用它们

LocationRedux.tsx

import * as Type from './LocationReduxTypes';
const initialState: Type.LocationState = {
    location: undefined,
};
const LocationRedux = (
    state = initialState,
    action: Type.LocationActionTypes,
): Type.LocationState => {
    switch (action.type) {
        case Type.SET_LOCATION:
            return { ...state, location: action.location };
        default:
            return state;
    }
};

export { LocationRedux as default };

import { MenuItemType } from '~/Helpers/Menu';
import { GeolocationResponse } from '@react-native-community/geolocation';

export const SET_LOCATION = 'SET_LOCATION';

interface SetLocationAction {
    type: typeof SET_LOCATION;
    location?: GeolocationResponse;
}

export interface LocationState {
    location?: GeolocationResponse;
}
export type LocationActionTypes = SetLocationAction;

locationReduxations.tsx
-这里我需要返回一个承诺,即当我使用此方法时,我需要知道位置已解决

import * as Type from './LocationReduxTypes';
import Store from '~/Redux/Store';

import { GeolocationResponse } from '@react-native-community/geolocation';

class LocationReduxActions {
    public static SetLocation(location: GeolocationResponse) {
        return new Promise((resolve, reject) => {
            resolve(
                Store.instance.dispatch({
                    type: Type.SET_LOCATION,
                    location,
                }),
            );
            reject(
                console.log('rejected'),
            );
        });

    }
}

export { LocationReduxActions as default };

LocationReduxType.tsx

import * as Type from './LocationReduxTypes';
const initialState: Type.LocationState = {
    location: undefined,
};
const LocationRedux = (
    state = initialState,
    action: Type.LocationActionTypes,
): Type.LocationState => {
    switch (action.type) {
        case Type.SET_LOCATION:
            return { ...state, location: action.location };
        default:
            return state;
    }
};

export { LocationRedux as default };

import { MenuItemType } from '~/Helpers/Menu';
import { GeolocationResponse } from '@react-native-community/geolocation';

export const SET_LOCATION = 'SET_LOCATION';

interface SetLocationAction {
    type: typeof SET_LOCATION;
    location?: GeolocationResponse;
}

export interface LocationState {
    location?: GeolocationResponse;
}
export type LocationActionTypes = SetLocationAction;

这就是我尝试使用这个动作的方式

    componentDidMount() {
        if (!this.props.location) {
            Geolocation.getCurrentPosition(location => {
                LocationReduxActions.SetLocation(location)
                .then(() => { // debugger never hit this then method.
                    this._load();
                });
            }, () => { // error handling.
                this._load();
            });
        } else { 
            this._load(); 
        }
    }

任何帮助都将不胜感激,谢谢。

我认为then被调用了,您确实返回了来自
SetLocation
的承诺,并解决了它

另外,请注意,
dispatch
是同步的,因此您不需要承诺:一旦调用存储,它就会更新


顺便说一句,如果你想根据商店里的东西更新你的UI,一个更简单和常见的方法是
react redux

我想如果调用了then,你确实是从
SetLocation
返回了一个承诺,并解决了它

另外,请注意,
dispatch
是同步的,因此您不需要承诺:一旦调用存储,它就会更新

顺便说一句,如果您想根据商店中的内容更新UI,一种更简单、通用的方法是
react-redux