React Native Cookbook Second Edition
Total Page:16
File Type:pdf, Size:1020Kb
React Native Cookbook Second Edition Recipes for solving common React Native development problems Dan Ward BIRMINGHAM - MUMBAI React Native Cookbook Second Edition Copyright © 2019 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Commissioning Editor: Amarabhab Banerjee Acquisition Editor: Trusha Shriyan Content Development Editor: Arun Nadar Technical Editor: Leena Patil Copy Editor: Safis Editing Project Coordinator: Kinjal Bari Proofreader: Safis Editing Indexer: Tejal Daruwale Soni Graphics: Alishon Mendonsa Production Coordinator: Arvindkumar Gupta First published: December 2016 Second edition: January 2019 Production reference: 2080419 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78899-192-6 www.packtpub.com mapt.io Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Mapt is fully searchable Copy and paste, print, and bookmark content Packt.com Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. Contributors About the author Dan Ward is a full-stack developer and web technology consultant who has a number of years of experience working on mobile applications with React Native, and developing web applications with React, Vue, and Angular. He's also a co-founder at gitconnected, and co- editor at the associated Medium publication. His professional interests include React Native development, modern web development, and technical writing. He also has a BA in English Literature from Florida State University. About the reviewer Ashok Kumar S has been working in the mobile development domain for about six years. In his early days, he was a JavaScript and Node.js developer. Thanks to his strong web development skills, he mastered web and mobile development. He is a Google-certified engineer, a speaker at global-scale conferences, including DroidCon Berlin and MODS, and also runs a YouTube channel called AndroidABCD for Android developers. He also contributes to open source heavily with a view to improving his e-karma. He has written books on Wear OS programming and Mastering Firebase Toolchain. He has also reviewed books on mobile and web development, namely, Mastering JUnit5, Android Programming for Beginners, and Building Enterprise JavaScript Applications. I would like to thank my family, mostly my mother, for her infinite support in every possible way, as well as family members Shylaja, Sumitra, Krishna, and Vinisha, and my fiancee, Geetha Shree. Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. Table of Contents Preface 1 Chapter 1: Setting Up Your Environment 8 Technical requirements 9 Installing dependencies 9 Installing Xcode 9 Installing Android Studio 12 How to do it... 12 There's more... 16 Installing Node.js 16 Installing Expo 17 Installing Watchman 18 Initializing your first app 18 Running your app in a simulator/emulator 19 Running your app on an iOS simulator 19 There's more... 21 Running your app on a real device 22 Running your app on an iPhone or Android 22 Summary 23 Further reading 23 Chapter 2: Creating a Simple React Native App 24 Adding styles to elements 24 Getting ready 25 How to do it... 25 How it works... 28 There's more... 29 Using images to mimic a video player 30 Getting ready 30 How to do it... 30 How it works... 34 Creating a toggle button 35 Getting ready 35 How to do it... 35 How it works... 40 There's more... 42 Displaying a list of items 42 Getting ready 42 How to do it... 42 How it works... 49 Table of Contents There's more... 50 Using flexbox to create a layout 50 Getting ready 51 How to do it... 51 How it works... 55 There's more... 56 See also 56 Setting up and using navigation 57 Getting ready 57 How to do it... 57 How it works... 65 See also 65 Chapter 3: Implementing Complex User Interfaces - Part I 66 Creating a reusable button with theme support 66 Getting ready 66 How to do it... 67 How it works... 72 Building a complex layout for tablets using flexbox 74 Getting ready 74 How to do it... 75 There's more... 83 See also 83 Including custom fonts 84 Getting ready 84 How to do it... 84 How it works... 92 See also 93 Using font icons 93 Getting ready 93 How to do it... 94 How it works... 98 See also 98 Chapter 4: Implementing Complex User Interfaces - Part II 99 Dealing with universal applications 99 Getting ready 100 How to do it... 100 How it works... 110 See also 111 Detecting orientation changes 111 Getting ready 111 How to do it... 112 There's more... 119 Using a WebView to embed external websites 119 Getting ready 120 [ ii ] Table of Contents How to do it... 120 How it works... 126 Linking to websites and other applications 127 Getting ready 127 How to do it... 128 How it works... 132 See also 133 Creating a form component 133 Getting ready 133 How to do it... 134 How it works... 139 Chapter 5: Implementing Complex User Interfaces - Part III 140 Introduction 140 Creating a map app 140 Getting ready 141 How to do it... 141 How it works... 145 There's more... 146 Creating an audio player 146 Getting ready 146 How to do it... 147 How it works... 154 There's more... 156 Creating an image carousel 156 Getting ready 157 How to do it... 157 How it works... 164 There's more... 164 Adding push notifications to your app 165 Getting ready 166 How to do it... 167 How it works... 180 There's more... 182 Implementing browser-based authentication 183 Getting ready 183 How to do it... 184 How it works... 189 See also 190 Chapter 6: Adding Basic Animations to Your App 191 Introduction 191 Creating simple animations 192 Getting ready 192 How to do it... 192 How it works... 196 [ iii ] Table of Contents Running multiple animations 198 Getting ready 198 How to do it... 198 How it works... 202 Creating animated notifications 203 Getting ready 203 How to do it... 203 How it works... 212 There's more... 213 Expanding and collapsing containers 213 Getting ready 213 How to do it... 214 How it works... 219 See also 220 Creating a button with a loading animation 220 Getting ready 220 How to do it... 220 How it works... 226 Conclusion 226 Chapter 7: Adding Advanced Animations to Your App 227 Introduction 227 Removing items from a list component 227 Getting ready 227 How to do it... 228 How it works... 235 See also 237 Creating a Facebook reactions widget 237 Getting ready 237 How to do it... 238 How it works... 244 Displaying images in fullscreen 245 Getting ready 246 How to do it... 246 How it works... 255 See also 255 Chapter 8: Working with Application Logic and Data 256 Introduction 256 Storing and retrieving data locally 257 Getting ready 257 How to do it... 257 How it works... 261 See also 262 Retrieving data from a remote API 262 [ iv ] Table of Contents Getting ready 263 How to do it... 263 How it works... 266 Sending data to a remote API 266 Getting ready 266 How to do it... 267 How it works... 271 Establishing real-time communication with WebSockets 272 Getting ready 272 How to do it... 273 How it works... 279 Integrating persistent database functionality with Realm 279 Getting ready 280 How to do it... 281 How it works... 285 Masking the application upon network connection loss 285 Getting ready 285 How to do it... 285 How it works... 290 Synchronizing locally persisted data with a remote API 291 Getting ready 291 How to do it..