Let's look at how to set up our app so that we can use our debugging tools. To start with, we'll require a simple change in the store creation code, adding a couple of lines, as shown here:
// Source file: src/regionsStyledApp/store.js
/* @flow */
import { createStore, applyMiddleware } from "redux";
import thunk from "redux-thunk";
import { composeWithDevTools } from "redux-devtools-extension";
import { reducer } from "./world.reducer";
export const store = createStore(
reducer,
composeWithDevTools(applyMiddleware(thunk))
);
Just for the sake of it—so that we can actually get some debugging messages – I added sundry console.log() and console.error() calls throughout the code. For consistency, I wanted to use debug (from https://www.npmjs.com/package/debug), as we did earlier in the book, but it won't work, because it requires LocalStorage, and in RN you get AsyncStorage instead, with a different API. Just as an example, we'll look at some log output from world.actions.js. I didn't bother logging the output from successful API calls, because we'll be getting that through react-native-debugger, as we'll see:
// Source file: src/regionsStyledApp/world.actions.js
.
.
.
export const getCountries = () => async dispatch => {
console.log("getCountries: called");
try {
dispatch(countriesRequest());
const result = await getCountriesAPI();
dispatch(countriesSuccess(result.data));
} catch (e) {
console.error("getCountries: failure!");
dispatch(countriesFailure());
}
};
export const getRegions = (country: string) => async dispatch => {
console.log("getRegions: called with ", country);
if (country) {
try {
dispatch(regionsRequest(country));
const result = await getRegionsAPI(country);
dispatch(regionsSuccess(result.data));
} catch (e) {
console.error("getRegions: failure with API!");
dispatch(regionsFailure());
}
} else {
console.error("getRegions: failure, no country!");
dispatch(regionsFailure());
}
};
We have everything in place; let's try it out.