Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
React and React Native

You're reading from   React and React Native A complete hands-on guide to modern web and mobile development with React.js

Arrow left icon
Product type Paperback
Published in Apr 2020
Publisher Packt
ISBN-13 9781839211140
Length 526 pages
Edition 3rd Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Adam Boduch Adam Boduch
Author Profile Icon Adam Boduch
Adam Boduch
Roy Derks Roy Derks
Author Profile Icon Roy Derks
Roy Derks
Arrow right icon
View More author details
Toc

Table of Contents (33) Chapters Close

Preface 1. Section 1: React
2. Why React? FREE CHAPTER 3. Rendering with JSX 4. Component Properties, State, and Context 5. Getting Started with Hooks 6. Event Handling - The React Way 7. Crafting Reusable Components 8. The React Component Life Cycle 9. Validating Component Properties 10. Handling Navigation with Routes 11. Code Splitting Using Lazy Components and Suspense 12. Server-Side React Components 13. User Interface Framework Components 14. Section 2: React Native
15. Why React Native? 16. Kick-Starting React Native Projects 17. Building Responsive Layouts with Flexbox 18. Navigating Between Screens 19. Rendering Item Lists 20. Showing Progress 21. Geolocation and Maps 22. Collecting User Input 23. Displaying Modal Screens 24. Responding to User Gestures 25. Controlling Image Display 26. Going Offline 27. Section 3: React Architecture
28. Native UI Components Using NativeBase 29. Handling Application State 30. Why Apollo? 31. Building an Apollo React App 32. Other Books You May Enjoy

Fetching list data

Often, you'll fetch your list data from some API endpoint. In this section, you'll learn about making API requests from React Native components. The good news is that the fetch() API is polyfilled by React Native, so the networking code in your mobile applications should look and feel a lot like it does in your web applications.

To start things off, let's build a mock API for our list items using functions that return promises just like fetch() does:

const items = new Array(100).fill(null).map((v, i) => `Item ${i}`);

function filterAndSort(data, text, asc) {
return data
.filter(i => text.length === 0 || i.includes(text))
.sort(
asc
? (a, b) => (b > a ? -1 : a === b ? 0 : 1)
: (a, b) => (a > b ? -1 : a === b ? 0 : 1)
);
}

export function fetchItems(filter, asc) {
return new Promise(resolve => {
resolve({
json: () =>
Promise.resolve({
items: filterAndSort(items, filter, asc)
...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime