Search icon CANCEL
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 Native By Example

You're reading from   React Native By Example Native mobile development with React

Arrow left icon
Product type Paperback
Published in Apr 2017
Publisher Packt
ISBN-13 9781786464750
Length 414 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Richard Kho Richard Kho
Author Profile Icon Richard Kho
Richard Kho
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. First Project - Creating a Basic To-Do List App FREE CHAPTER 2. Advanced Functionality and Styling the To-Do List App 3. Our Second Project - The Budgeting App 4. Advanced Functionality with the Expenses App 5. Third Project - The Facebook Client 6. Advanced Facebook App Functionality 7. Redux 8. Deploying Your Applications 9. Additional React Native Components

StyleSheet

React Native's core visual components accept a prop called style and the names and values more or less match up with CSS's naming conventions, with one major exception--kebab-case is swapped out for camelCase, similar to how things are named in JavaScript. For example, a CSS property of background-color will translate to backgroundColor in React Native.

For readability and reuse, it's beneficial to break off inline styling into its own styles object by defining all of our styles into a styles object using React Native's StyleSheet component to create a style object and reference it within our component's render method.

Taking it a step further, with larger applications, it's best to separate the style sheet into its own JavaScript file for readability's sake. Let's take a look at how each of these compare, using a very annotated version of the Hello World sample that's generated for us. These samples will contain only the code necessary to make my point.

Inline styles

An inline style is one that is defined within the markup of your code. Check this sample out:

class Tasks extends Component { 
render () {
return (
<View style = {{ flex: 1, justifyContent: 'center',
alignItems: 'center', backgroundColor: '#F5FCFF'
}}>
<Text style = {{ fontSize: 20, textAlign:
'center', margin: 10 }}>
Welcome to React Native!
</Text>
</View>
)
}
}

In the preceding code, you can see how inline style can create a very convoluted and confusing mess, especially when there are several style properties that we want to apply to each component. It's not practical for us to write our styles like this in a large-scale application, so let's break apart the styles into a StyleSheet object.

With StyleSheet, within the same file

This is how a component accesses a StyleSheet created in the same file:

class Tasks extends Component { 
render () {
return (
<View style = { styles.container }>
<Text style = { styles.welcome }>
Welcome to React Native!
</Text>
</View>
)
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF'
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10
}
)};

This is much better. We're moving our styles into an object we can reference without having to rewrite the same inline styles over and over. However, the problem we face is an extraordinarily long file with a lot of application logic, where a future maintainer might have to scroll through lines and lines of code to get to the styles. We can take it one step further and separate the styles into their own module.

StyleSheet as an imported module

In your component, you can import your styles, as shown:

import styles from './styles.js'; 

class Tasks extends Component {
render(){
return (
<View style = { styles.container }>
<Text style = { styles.welcome }>
Welcome to React Native!
</Text>
</View>
)
}
}

Then, you can define them in a separate file:

const styles = StyleSheet.create({ 
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF'
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10
}
)};

export default styles;

This is much better. By encapsulating our style logic into its own file, we are separating our concerns and making it easier for everyone to read it.

You have been reading a chapter from
React Native By Example
Published in: Apr 2017
Publisher: Packt
ISBN-13: 9781786464750
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 €18.99/month. Cancel anytime