Skip to content

Commit 6a31a57

Browse files
getDerivedStateFromProps, could use reorganizing
1 parent 6b4953c commit 6a31a57

File tree

5 files changed

+68
-60
lines changed

5 files changed

+68
-60
lines changed

.eslintrc

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"no-prototype-builtins": "off",
4848
"no-bitwise": "off",
4949
"no-console": "off",
50+
"no-nested-ternary": "off",
5051
"no-underscore-dangle": "off",
5152
"object-curly-newline": "off",
5253
"no-use-before-define": "off",

components/frogimage.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,24 @@ import contextWrap from '../util/contextWrap';
55
import FrogVideo from './FrogVideo';
66

77
class FrogImage extends React.Component {
8-
state = { currentDude: 0 };
8+
state = {
9+
currentDude: 0,
10+
};
11+
12+
static getDerivedStateFromProps({ context: { REEEEE, notWednesday, notWednesdayDude, isWednesday, todaysDudes, isLoading } }, prevState) {
13+
return {
14+
currentDude: prevState.currentDude,
15+
REEEEE,
16+
notWednesday,
17+
notWednesdayDude,
18+
isWednesday,
19+
todaysDudes,
20+
isLoading,
21+
};
22+
}
23+
924
render() {
10-
const { currentDude } = this.state;
11-
const { REEEEE, notWednesday, notWednesdayDude, isWednesday, todaysDudes, isLoading } = this.props.context;
25+
const { currentDude, REEEEE, notWednesday, notWednesdayDude, isWednesday, todaysDudes, isLoading } = this.state;
1226
return !isLoading
1327
?
1428
<TouchableOpacity onPress={

contexts/GlobalProvider.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ export default class GlobalProvider extends Component {
3636
}
3737
const notWedUrl = `https://graph.facebook.com/v3.0/1726444857365752/photos?fields=images&access_token=${this.state.token}`;
3838
const curDate = (new Date()).toLocaleDateString();
39-
const { dudesRepository, lastFetched } = storageObj;
40-
if (!dudesRepository || !dudesRepository.length) {
41-
await this.cacheFroggos();
42-
}
39+
const { lastFetched } = storageObj;
4340
if (this.state.isWednesday && (lastFetched !== curDate)) {
4441
await this.fetchFroggos();
4542
}
@@ -60,8 +57,8 @@ export default class GlobalProvider extends Component {
6057
onPress: async () => {
6158
const allKeys = await AsyncStorage.getAllKeys();
6259
await AsyncStorage.multiRemove(allKeys);
63-
this.setState({ dudesCollection: [], godmode: false, isWednesday: this.state.trueWednesday });
64-
await this.cacheFroggos();
60+
this.setState({ dudesCollection: [], todaysDudes: [], godmode: false, isWednesday: this.state.trueWednesday });
61+
await this.fetchFroggos();
6562
} },
6663
],
6764
{ cancelable: false }
@@ -72,11 +69,11 @@ export default class GlobalProvider extends Component {
7269
this.setState({ isLoading: true });
7370
const { todaysDudes } = this.state;
7471
try {
75-
const dudesRepository = JSON.parse(await AsyncStorage.getItem('dudesRepository')) || [];
72+
if (!this.state.dudesRepository) await this.cacheFroggos();
7673
for (let i = 0; i < 5; i++) {
77-
const randomIndex = (Math.random() * dudesRepository.length | 0);
74+
const randomIndex = (Math.random() * this.state.dudesRepository.length | 0);
7875
// Implement categorization strats here
79-
const loopDude = dudesRepository[randomIndex];
76+
const loopDude = this.state.dudesRepository[randomIndex];
8077
const { id } = loopDude;
8178
const { source } = loopDude.images[0];
8279
const thumbnail = findThumbnailDude(loopDude.images);
@@ -115,14 +112,15 @@ export default class GlobalProvider extends Component {
115112
dudesRepository = dudesRepository.concat(response.data);
116113
url = response.paging.next;
117114
}
115+
this.setState({ dudesRepository });
118116
await AsyncStorage.setItem('dudesRepository', JSON.stringify(dudesRepository));
119117
}
120118

121119
_toggleGodmode = async () => {
122120
if (!this.state.trueWednesday) {
123121
console.log('toggling godmode');
124122
const godmode = !this.state.godmode;
125-
this.setState({ godmode });
123+
this.setState({ godmode, isLoading: true });
126124
const lastFetched = await AsyncStorage.getItem('lastFetched');
127125
const curDate = (new Date()).toLocaleDateString();
128126
if (godmode && (lastFetched !== curDate)) {

screens/Collection.js

+27-33
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { Component } from 'react';
2-
import { StyleSheet, View, Image, Text, Platform, AsyncStorage } from 'react-native';
2+
import { StyleSheet, View, Image, Text, Platform, ActivityIndicator } from 'react-native';
33
import GridView from 'react-native-super-grid';
44
import Lightbox from 'react-native-lightbox';
55
import contextWrap from '../util/contextWrap';
@@ -11,44 +11,38 @@ class Collection extends Component {
1111

1212
state = {
1313
dudesCollection: this.props.context.dudesCollection,
14+
isLoading: this.props.context.isLoading,
1415
}
1516

16-
componentWillReceiveProps(nextProps) {
17-
const { dudesCollection } = nextProps.context;
18-
this.setState({ dudesCollection });
19-
}
20-
21-
// Hack until I figure out contexutal state issues
22-
componentDidMount() {
23-
this.props.navigation.addListener(
24-
'didFocus',
25-
async () => {
26-
const dudesCollection = JSON.parse(await AsyncStorage.getItem('dudesCollection')) || [];
27-
this.setState({ dudesCollection });
28-
}
29-
);
17+
static getDerivedStateFromProps({ context: { dudesCollection, isLoading } }) {
18+
return {
19+
dudesCollection,
20+
isLoading,
21+
};
3022
}
3123

3224
render() {
33-
const { dudesCollection } = this.state;
34-
return dudesCollection.length
35-
?
36-
<GridView
37-
itemDimension={130}
38-
items={dudesCollection}
39-
style={styles.gridView}
40-
renderItem={({ thumbnail }) => (
41-
<Lightbox>
42-
<View style={styles.itemContainer}>
43-
<Image source={{ uri: thumbnail }} style={styles.backgroundImage} />
44-
</View>
45-
</Lightbox>
25+
const { dudesCollection, isLoading } = this.state;
26+
return !isLoading
27+
? dudesCollection.length
28+
?
29+
<GridView
30+
itemDimension={130}
31+
items={dudesCollection}
32+
style={styles.gridView}
33+
renderItem={({ thumbnail }) => (
34+
<Lightbox>
35+
<View style={styles.itemContainer}>
36+
<Image source={{ uri: thumbnail }} style={styles.backgroundImage} />
37+
</View>
38+
</Lightbox>
4639
)}
47-
/>
48-
:
49-
<View style={styles.tabBarInfoContainer}>
50-
<Text style={styles.tabBarInfoText}>No Frogs to see here just yet.</Text>
51-
</View>;
40+
/>
41+
:
42+
<View style={styles.tabBarInfoContainer}>
43+
<Text style={styles.tabBarInfoText}>No Frogs to see here just yet.</Text>
44+
</View>
45+
: <ActivityIndicator size="large" color="#0000ff" />;
5246
}
5347
}
5448

screens/Settings.js

+15-14
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,22 @@ class SettingsScreen extends Component {
88
};
99

1010
state = {
11-
godmode: false,
12-
listViewData: [
13-
{
14-
key: 'toggleGodmode', displayItems: ['It\'s Always Wednesday in Philadelphia'], type: 'toggle'
15-
},
16-
{
17-
key: 'clearDudes', displayItems: ['Clear Dudes'], type: 'alert',
18-
},
19-
],
20-
};
11+
godmode: this.props.context.godmode,
12+
listViewData: [],
13+
}
2114

22-
// Unsafe, deprecated, probably an anti-pattern
23-
componentWillReceiveProps(nextProps) {
24-
const { godmode } = nextProps.context;
25-
this.setState({ godmode });
15+
static getDerivedStateFromProps(nextProps) {
16+
return {
17+
godmode: nextProps.context.godmode,
18+
listViewData: [
19+
{
20+
key: 'toggleGodmode', displayItems: ['It\'s Always Wednesday in Philadelphia'], type: 'toggle'
21+
},
22+
{
23+
key: 'clearDudes', displayItems: ['Clear Dudes'], type: 'alert',
24+
},
25+
],
26+
};
2627
}
2728

2829
_renderItem = data => {

0 commit comments

Comments
 (0)