-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
112 lines (103 loc) · 2.49 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import * as React from 'react';
import {SafeAreaView, ScrollView, StyleSheet, Text, View} from 'react-native';
import axios from 'axios';
interface AppProps {}
interface AppState {
page: number;
totoalPageSize: number;
hitsApi: {title: string; author: string; url: string}[];
}
class App extends React.Component<AppProps, AppState> {
constructor(props: AppProps) {
super(props);
this.state = {
page: 0,
hitsApi: [],
totoalPageSize: 0,
};
}
handleFetch = async () => {
const res = await axios.get(
`https://hn.algolia.com/api/v1/search_by_date?tags=story&page=${this.state.page}`,
);
console.log('====================================');
console.log({res});
console.log('====================================');
this.setState({
page: this.state.page + 1,
totoalPageSize: res.data.nbPages,
hitsApi: [...this.state.hitsApi, ...res.data.hits],
});
};
timer: any;
componentDidMount() {
this.handleFetch();
this.timer = setInterval(() => {
this.state.page < this.state.totoalPageSize && this.handleFetch();
}, 5000);
}
componentWillUnmount(): void {
clearInterval(this.timer);
}
render() {
console.log({...this.state});
return (
<SafeAreaView>
<ScrollView>
<View style={styles.viewmain}>
{this.state.hitsApi.map((hits, i) => {
return (
<View style={styles.card} key={hits.title + i}>
<View>
<Text style={styles.text1}>{hits.title}</Text>
<Text style={styles.text2}>{hits.author}</Text>
</View>
</View>
);
})}
</View>
</ScrollView>
</SafeAreaView>
);
}
}
const styles = StyleSheet.create({
viewmain: {
flexDirection: 'row',
flexWrap: 'wrap',
width: '100%',
marginRight: 'auto',
marginLeft: 'auto',
marginTop: 10,
},
card: {
width: '30%',
marginRight: '1%',
marginBottom: '2%',
marginLeft: '1%',
shadowColor: '#000',
shadowOffset: {
width: 0,
height: 1,
},
shadowOpacity: 0.22,
shadowRadius: 2.22,
elevation: 3,
borderWidth: 1,
borderRadius: 10,
height: 150,
padding: 10,
},
text1: {
fontSize: 12,
fontWeight: '900',
textAlign: 'center',
textTransform: 'uppercase',
},
text2: {
fontSize: 10,
fontWeight: '500',
textTransform: 'lowercase',
},
});
export default App;