Hands-On Design Patterns with React Native
eBook - ePub

Hands-On Design Patterns with React Native

Proven techniques and patterns for efficient native mobile development with JavaScript

  1. 302 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Hands-On Design Patterns with React Native

Proven techniques and patterns for efficient native mobile development with JavaScript

About this book

Learn how to write cross platform React Native code by using effective design patterns in the JavaScript world. Get to know industry standard patterns as well as situational patterns. Decouple your application with these set of "Idea patterns".

Key Features

  • Mobile development in React Native should be done in a reusable way.
  • Learn how to build scalable applications using JavaScript patterns that are battle tested.
  • Try effective techniques on your own using over 80 standalone examples.

Book Description

React Native helps developers reuse code across different mobile platforms like iOS and Android.

This book will show you effective design patterns in the React Native world and will make you ready for professional development in big teams.

The book will focus only on the patterns that are relevant to JavaScript, ECMAScript, React and React Native. However, you can successfully transfer a lot of the skills and techniques to other languages. I call them "Idea patterns".

This book will start with the most standard development patterns in React like component building patterns, styling patterns in React Native and then extend these patterns to your mobile application using real world practical examples. Each chapter comes with full, separate source code of applications that you can build and run on your phone.

The book is also diving into architectural patterns. Especially how to adapt MVC to React environment. You will learn Flux architecture and how Redux is implementing it. Each approach will be presented with its pros and cons. You will learn how to work with external data sources using libraries like Redux thunk and Redux Saga.

The end goal is the ability to recognize the best solution for a given problem for your next mobile application.

What you will learn

  • Explore the design Patterns in React Native
  • Learn the best practices for React Native development
  • Explore common React patterns that are highly used within React Native development
  • Learn to decouple components and use dependency injection in your applications
  • Explore the best ways of fetching data from the backend systems
  • Learn the styling patterns and how to implement custom mobile designs
  • Explore the best ways to organize your application code in big codebases

Who this book is for

The ideal target audience for this book are people eager to learn React Native design patterns who already know the basics of JavaScript. We can assume that the target audience already knows how to write Hello World in JavaScript and know what are the functions, recursive functions, JavaScript types and loops.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Styling Patterns

It's time to add some looks to our applications. In this chapter, we will explore unique styling solutions and mechanisms. React Native StyleSheet may resemble web cascading style sheets (CSS); however, Native application styling is different. Similarities in the syntax quickly end and you should spend some time with this chapter to learn the basics of styling. Later on in this book, we will use an external library that provides ready-made styles. It is crucial for you to understand how to make such components yourself, especially if you plan to work professionally in React Native in teams who deliver custom designs.
In this chapter, we will cover the following topics:
  • Styling components in the React Native environment
  • Dealing with limited style inheritance
  • Using density-independent pixels
  • Positioning elements with Flexbox
  • Handling long text issues
  • Making animations using the Animated library
  • Measuring your application's speed using the Frames Per Second (FPS) metric

Technical requirements

As in the previous chapters, I have separated every example into a standalone application that you can launch on your phone or simulator. To do the examples, you will need the following:
  • Simulator or Android/iOS smartphone
  • Git to pull the examples: https://github.com/Ajdija/hands-on-design-patterns-with-react-native. Follow the installation instructions from the GitHub page.

How React Native styles work

"The core premise for React is that UIs are simply a projection of data into a different form of data. The same input gives the same output. A simple pure function."

- React library README (https://github.com/reactjs/react-basic/blob/master/README.md).
You will learn about pure functions later in this book. Check out the following example to understand the basics:
// Code example from React readme. Comments added for clarity.

// JavaScript pure function
// for a given input always returns the same output
function
NameBox(name) {
return { fontWeight: 'bold', labelContent: name };
}

// Example with input
'Sebastian Markbåge' ->
{ fontWeight: 'bold', labelContent: 'Sebastian Markbåge' };
Going back to more practical examples, let's see how the preceding premise is implemented in React Native.
"With React Native, you don't use a special language or syntax for defining styles. You just style your application using JavaScript. All of the core components accept a prop named style. The style names and values usually match how CSS works on the web, except names are written using camel casing, e.g backgroundColor rather than background-color.

The style prop can be a plain old JavaScript object. (...) You can also pass an array of styles - the last style in the array has precedence, so you can use this to inherit styles.

As a component grows in complexity, it is often cleaner to use StyleSheet.create to define several styles in one place."

- React Native official documentation (https://facebook.github.io/react-native/docs/style.html).
To sum up, we have three ways of defining the component style:
  • Using style props and passing an object with key-value pairs that represent styles.
  • Using style props and passing an array of objects. Each object should contain key-value pairs that represent styles. The last style in the array has precedence. Use this mechanism to inherit styles or shadow them as you would shadow functions and variables.
  • Using the StyleSheet component and its create function to create styles.
In the following example, you can find all three ways of defining styles:
// src/ Chapter_3/ Example_1_three_ways_to_define_styles/ App.js

export default
() => (
&l...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Dedication
  4. Packt.com
  5. Contributors
  6. Preface
  7. React Component Patterns
  8. View Patterns
  9. Styling Patterns
  10. Flux Architecture
  11. Store Patterns
  12. Data Transfer Patterns
  13. Navigation Patterns
  14. JavaScript and ECMAScript Patterns
  15. Elements of Functional Programming Patterns
  16. Managing Dependencies
  17. Type Checking Patterns
  18. Other Books You May Enjoy

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Hands-On Design Patterns with React Native by Mateusz Grzesiukiewicz in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in JavaScript. We have over one million books available in our catalogue for you to explore.