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

Book details
Book preview
Table of contents
Citations

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.

Frequently asked questions

Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
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 1000+ topics, we’ve got you covered! Learn more here.
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 here.
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 Mobile Devices. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781788999540
Edition
1

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