In my last post, I took a look at how network latency might affect my React Native app, and I didn’t like what I saw: a pure white screen for several seconds. How is a user supposed to “react” to that? (Pun intended!)
It was pretty clear what was happening. My app’s render
method just shows a blank view if it has no state. Here’s the render
method:
render() {
if (this.state && !this.state.isLoading) {
...
} else {
return (<View></View>);
}
}
That’s my white screen. There’s a super simple fix:
render() {
if (this.state && !this.state.isLoading) {
...
} else {
return (<View><Text>waiting...</Text></View>);
}
}
Now there’s text which reads “waiting…” at the top. That’s okay for a hobby app. You’re probably going to want that to look a little nicer, however. How about a progress indicator? Something that shows “we’re working on this!”. React Native comes with an ActivityIndicator
which does what we want. I’m going to grab some of the code from their documentation and add it to my app. In the render
method below, I’ve replaced my “waiting” text with an ActivityIndicator
(and I’ve shown the import
statement to remind you to add that as well).
import { View, Text, Picker, ActivityIndicator } from 'react-native';
...
render() {
if (this.state && !this.state.isLoading) {
...
} else {
return (<View><ActivityIndicator size="large" color="#0000ff" /></View>);
}
}
That helps some, but the progress indicator still doesn’t look very nice. It’s placed at the very top of the screen, like this:
It turns out to be pretty easy to move the ActivityIndicator
to the center of the screen. You just have to add some style components to its container View
, like this:
render() {
if (this.state && !this.state.isLoading) {
...
} else {
return (<View style={[{ flex: 1, justifyContent: 'center' },
{ flexDirection: 'row', justifyContent: 'space-around', padding: 10 }]}>
<ActivityIndicator size="large" color="#0000ff" />
</View>);
}
}
Here’s the result:
With very little trouble at all, I’ve now got a slick little widget to let people know that something is going on. It’s not really enough, though. What if the network request times out, and I get an error? I’ll have to delve into that more deeply later. To remind me to do this, I’ll add a TODO/FIXME
comment in the code, and open an issue in my issue tracker.