19
loading...
This website collects cookies to deliver better user experience
E:\NMS\CURSOS\REACT-NATIVE-NINJA\react-native-tutorial
npm install --global expo-cli
expo init myproject
npm start
lesson-2 to lesson-8
: setup and basicslesson-9 to lesson-15
: Todo Applesson-16 to lesson-35
: The reviews AppIt emulates the idea of css.
views
are used instead of div
.View
, Text
, TextInput
, etc.<ScrollView>
<Button title='update state' onPress={clickHandler}/>
<TextInput
multiline
keyboardType='numeric'
placeholder='e.g. John Doe'
onChangeText={()=>setName()}
style={styles.input}/>
<ScrollView/>
<View style={styles.container}>
<FlatList
numColumns={2}
keyExtractor={(item) => item.id}
data={people}
renderItem={({ item }) => (
<TouchableOpacity onPress={()=>pressHandler(item.id)}>
<Text style={styles.item}>{item.name}</Text>
</TouchableOpacity>
)}
/>
</View>
// jsx
Alert.alert('OOPS', 'Todo must be over 3 characters long', [
{text: 'Understood', onPress: () => console.log('alert closed') }
]);
<TouchableWithoutFeedback onPress={() => {
Keyboard.dismiss();
console.log('dismissed');
}}>
<View style={styles.container}>
<Header />
<View style={styles.content}>
<AddTodo submitHandler={submitHandler} />
<View style={styles.list}>
<FlatList
data={todos}
renderItem={({ item }) => (
<TodoItem item={item} pressHandler={pressHandler} />
)}
/>
</View>
</View>
</View>
</TouchableWithoutFeedback>
export default function Sandbox() {
return (
<View style={styles.container}>
<Text style={styles.boxOne}>one</Text>
<Text style={styles.boxTwo}>two</Text>
<Text style={styles.boxThree}>three</Text>
<Text style={styles.boxFour}>four</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
paddingTop: 40,
backgroundColor: '#ddd',
flexDirection: 'row',
justifyContent: 'space-around',
alignItems: 'center',
height: '100%',
},
boxOne: {
flex: 1,
backgroundColor: 'violet',
padding: 10,
},
boxTwo: {
flex: 2,
backgroundColor: 'gold',
padding: 10,
alignSelf: 'flex-end',
},
boxThree: {
flex: 1,
backgroundColor: 'coral',
padding: 10,
},
boxFour: {
flex: 3,
backgroundColor: 'skyblue',
padding: 10,
alignSelf: 'flex-start',
}
});
import { MaterialIcons } from '@expo/vector-icons';
export default function TodoItem({ pressHandler, item }) {
return (
<TouchableOpacity onPress={() => pressHandler(item.key)}>
<View style={styles.item}>
<MaterialIcons name='delete' size={18} color='#333' />
<Text style={styles.itemText}>{item.text}</Text>
</View>
</TouchableOpacity>
)
}
expo install expo-font @expo-google-fonts/nunito
npm install expo-app-loading
import React from 'react';
import AppLoading from 'expo-app-loading';
import Home from './screens/home';
import { useFonts, Nunito_400Regular, Nunito_700Bold } from '@expo-google-fonts/nunito';
export default function App() {
let [fontsLoaded] = useFonts({
Nunito_400Regular,
Nunito_700Bold,
});
if (!fontsLoaded) {
return <AppLoading />;
}
else {
return <Home />;
}
}
import { StyleSheet } from 'react-native';
export const globalStyles = StyleSheet.create({
titleText: {
fontSize: 18,
fontWeight: 'bold',
color: '#333',
},
paragraph: {
marginVertical: 8,
lineHeight: 20,
},
container: {
flex: 1,
padding: 20,
},
});
# install react-navigation 5.0
npm install @react-navigation/native
npm install @react-navigation/stack
expo install react-native-gesture-handler react-native-reanimated react-native-screens react-native-safe-area-context @react-native-community/masked-view
npm install react-navigation-stack
routes/homeStack.js
:import React from "react";
import { NavigationContainer } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import Home from "../screens/home";
import ReviewDetails from "../screens/reviewDetails";
const { Navigator, Screen } = createStackNavigator();
const HomeNavigator = () => (
<Navigator headerMode="none">
{/* //other options: "float", "screen" */}
<Screen name="Details" component={ReviewDetails} />
<Screen name="Home" component={Home} />
</Navigator>
);
export const AppNavigator = () => (
<NavigationContainer>
<HomeNavigator />
</NavigationContainer>
);
...
import { AppNavigator } from "./routes/HomeStack";
...
if (fontsLoaded) {
return <AppNavigator />;
} else {
...
}
export default function Home({ navigation }) {
const pressHandler = () => {
navigation.navigate('ReviewDetails');
// navigation.goBack();
// navigation.push('ReviewDetails');
}
return (
<View style={globalStyles.container}>
<Text style={globalStyles.titleText}>Home Screen</Text>
<Button title='to review details screen' onPress={pressHandler} />
</View>
);
}
npm install @react-navigation/drawer
./routes/drawer.js
import React from 'react';
import { createDrawerNavigator } from '@react-navigation/drawer';
import { NavigationContainer } from '@react-navigation/native';
import { HomeNavigator } from './homeStack'
import { AboutNavigator } from './aboutStack';
const Drawer = createDrawerNavigator();
export const AppDrawer = () => {
return (
<NavigationContainer>
<Drawer.Navigator headerMode="screen" initialRouteName="Home">
<Drawer.Screen name="Home" component={HomeNavigator} />
<Drawer.Screen name="About" component={AboutNavigator} />
</Drawer.Navigator>
</NavigationContainer>
);
}
npm install formik
import React from 'react';
import { StyleSheet, Button, TextInput, View, Text } from 'react-native';
import { globalStyles } from '../styles/global.js';
import { Formik } from 'formik';
import * as yup from 'yup';
import FlatButton from '../shared/button.js';
const reviewSchema = yup.object({
title: yup.string()
.required()
.min(4),
body: yup.string()
.required()
.min(8),
rating: yup.string()
.required()
.test('is-num-1-5', 'Rating must be a number 1 - 5', (val) => {
return parseInt(val) < 6 && parseInt(val) > 0;
}),
});
export default function ReviewForm({ addReview }) {
return (
<View style={globalStyles.container}>
<Formik
initialValues={{ title: '', body: '', rating: '' }}
validationSchema={reviewSchema}
onSubmit={(values, actions) => {
actions.resetForm();
addReview(values);
}}
>
{props => (
<View>
<TextInput
style={globalStyles.input}
placeholder='Review title'
onChangeText={props.handleChange('title')}
onBlur={props.handleBlur('title')}
value={props.values.title}
/>
{/* only if the left value is a valid string, will the right value be displayed */}
<Text style={globalStyles.errorText}>{props.touched.title && props.errors.title}</Text>
<FlatButton onPress={props.handleSubmit} text='submit' />
</View>
)}
</Formik>
</View>
);
}