I am trying to get location from redux when needed. After redux save the location to the state i need to return a promise since i need that data in my screen.
here are my actions, reducers, store, and how i use them.
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 };
LocationReduxActions.tsx
- here i need to return a promise that when i use this method, i need to know location is resolved.
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 { 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;
and this is how i am trying to use this action.
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();
}
}
any help will be appreciated, thanks.