Understanding the Mobile Landscape
The benefit of React Native is that if you understand how to build a React Native app for iOS, you understand how to build a React Native app for Android.
Let's go through this article
Learn more here
Using Expo
When installing globally, you will likely need to use 'sudo'
You can select the blank template.
Install Expo Client from the App Store
Scanning the QR code to see the Hello World App (same wifi network)
You can still use console.log()
Shake your device to see the debug and reload tools.
React Native Fundamentals
React Native, like React,js, is component-based.
You can use both functional components and class components in React Native, just like React.js
However, React Native does not create HTML with the help of JSX. Instead, we will need to use React Native Components.
In React
import React, { Component } from 'react';
class HelloWorldApp extends React {
render(){
return (
<h1>Hello World</h1>
)
}
}
becomes....
import React, { Component } from 'react';
import {Text} from 'react-native';
class HelloWorldApp extends Component{
render(){
return (
<Text>Hello World</Text>
)
}
}
In React
import React, { Component } from 'react';
export default function App() {
return (
<h1>Hello World</h1>
)
}
becomes....
import React, { Component } from 'react';
import { Text } from 'react-native';
export default function App(){
return (
<Text>Hello World</Text>
)
}
Docs here
Create the following:
CSS - Yes
Cascading? - No
We use a StyleSheet from react-native to create the styles. The styling you add will only apply to that component.
All components have a style prop that accepts a JavaScript object.
You can use Stylesheet.create() to create a variable that is used to style your components.
import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';
const styles = StyleSheet.create({
red: {
padding: 10,
},
medium: {
fontSize: '12px'
}
});
class LotsOfStyles extends Component {
render() {
return (
<View>
<Text style={styles.red}>just red</Text>
<Text style={[styles.medium, styles.red]}>medium and red</Text>
</View>
);
}
}
Styles have to be written in JS syntax
const styles = StyleSheet.create({
blue: {
color: 'blue',
fontWeight: 'bold',
fontSize: 30,
},
red: {
color: 'red',
}
});
Add styling to each one of the elements in your App component.
This will center the items in the middle of the screen.
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
},
});
Understanding flex here
Very much the same as React.js
//Colorbox.js
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
const ColorBox = props => {
return (
<View>
<Text>
{props.colorName} {props.hexCode}
</Text>
</View>
);
};
//Home.js
<ColorBox hexCode="#2aa198" colorName="Cyan" />
Want to use a map to create a list of components or strings? Sadly, no.
While we use .map() on the web, in React Native you should avoid using map in the render. This is because mapping over an array is not optimized. React Native will attempt to render every single element in the array all at once, regardless of whether they are visible on the screen or not.
Parent Component
//Home.js
import React from 'react';
import { View, Text, StyleSheet, FlatList } from 'react-native';
import ColorBox from './components/ColorBox';
const COLORS = [
{ colorName: 'red' },
{ colorName: 'green' },
{ colorName: 'blue' },
{ colorName: 'yellow'},
{ colorName: 'purple' },
];
const App = () => {
return (
<View>
<FlatList
data={COLORS}
keyExtractor={item => item.colorName}
renderItem={({ item }) => ( <ColorBox colorName={item.colorName} />)}
/>
</View>
);
const styles = StyleSheet.create({
fontWeight: 'bold',
marginBottom: 10,
});
export default App;
Child Component
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
const ColorBox = props => {
return (
<View>
<Text>{props.colorName}</Text>
</View>
);
}
export default ColorBox;
More docs here
Create the following FlatList. You can use whichever colors you would like.
brew install watchman
sudo gem install cocoapods
Routing and Navigation here
Let's go through this.
Understand the Xcode basics here
Xcode 12 updates here
Thank you for your attention!