MCPcopy Create free account
hub / github.com/dabbott/react-native-express / App

Function App

examples/files/app/networking.js:12–54  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

10import { actionCreators, initialState, reducer } from './posts'
11
12export 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}>

Callers

nothing calls this directly

Calls 1

fetchPostsFunction · 0.85

Tested by

no test coverage detected