()
| 10 | import { actionCreators, initialState, reducer } from './posts' |
| 11 | |
| 12 | export default function App() { |
| 13 | const [state, dispatch] = useReducer(reducer, initialState) |
| 14 | |
| 15 | useEffect(() => { |
| 16 | async function fetchPosts() { |
| 17 | dispatch(actionCreators.loading()) |
| 18 | |
| 19 | try { |
| 20 | const response = await fetch( |
| 21 | 'https://jsonplaceholder.typicode.com/posts' |
| 22 | ) |
| 23 | const posts = await response.json() |
| 24 | dispatch(actionCreators.success(posts)) |
| 25 | } catch (e) { |
| 26 | dispatch(actionCreators.failure()) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | fetchPosts() |
| 31 | }, []) |
| 32 | |
| 33 | const { posts, loading, error } = state |
| 34 | |
| 35 | if (loading) { |
| 36 | return ( |
| 37 | <View style={styles.center}> |
| 38 | <ActivityIndicator animating={true} /> |
| 39 | </View> |
| 40 | ) |
| 41 | } |
| 42 | |
| 43 | if (error) { |
| 44 | return ( |
| 45 | <View style={styles.center}> |
| 46 | <Text>Failed to load posts!</Text> |
| 47 | </View> |
| 48 | ) |
| 49 | } |
| 50 | |
| 51 | return ( |
| 52 | <FlatList |
| 53 | style={styles.container} |
| 54 | keyExtractor={(post) => post.id} |
| 55 | data={posts} |
| 56 | renderItem={({ item: { id, title, body }, index }) => ( |
| 57 | <View key={id} style={styles.post}> |
nothing calls this directly
no test coverage detected