Docsity
Trusted Seller

React native - esercizi, Esercizi di Sviluppo di Applicazioni Web

Toggle sidebar
bg1
ESERCIZI REACT NATIVE
INSTAGRAM 1
App 1
HomeScreen 1
SEARCH BAR 10
selected 10
SearchBarFilter 11
SearchBar 14
ListHorizontal 14
index 16
Heart 16
FetchLike 17
Fetch 19
App 21
ToDoList 22
Card 25
useSerieList 27
SearchBar 28
TIMER 30
App 30
INSTAGRAM
App
import React from 'react';
import { View } from 'react-native';
import HomeScreen from './src/HomeScreen';
const App = () => {
return (
<View style={{ flex: 1 }}>
<HomeScreen />
</View>
);
};
export default App;
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Anteprima parziale del testo

Scarica React native - esercizi e più Esercizi in PDF di Sviluppo di Applicazioni Web solo su Docsity!

ESERCIZI REACT NATIVE

INSTAGRAM 1

App 1 HomeScreen 1 SEARCH BAR 10 selected 10 SearchBarFilter 11 SearchBar 14 ListHorizontal 14 index 16 Heart 16 FetchLike 17 Fetch 19 App 21 ToDoList 22 Card 25 useSerieList 27 SearchBar 28 TIMER 30 App 30

INSTAGRAM

App

import React from 'react'; import { View } from 'react-native'; import HomeScreen from './src/HomeScreen'; const App = () => { return ( <View style={{ flex: 1 }}> ); }; export default App;

HomeScreen

import React, { useState } from 'react'; import { View, Text, ScrollView, StyleSheet, Image, TouchableOpacity, SafeAreaView } from 'react-native'; import Icon from 'react-native-vector-icons/FontAwesome'; // Componente per la barra superiore const HeaderBar = () => { return ( Instagram ); }; // Componente per le storie const Stories = () => { const storiesData = [ { image: 'https://th.bing.com/th/id/OIP.8t2B_cCHUZpjRUnTD03SYAHaEI?pid=ImgDet&rs=1', name: 'Utente1' }, { image: 'https://th.bing.com/th/id/OIP.BZ-zgZ3HkU652N1oZ_n5OgHaE8?pid=ImgDet&w=720&h= &rs=1', name: 'Utente2' }, { image: 'https://th.bing.com/th/id/OIP.dUngPLt8xS2GSkAmCwIQmQHaEI?pid=ImgDet&rs=1', name: 'Utente3' }, // ... altre storie ]; return ( {storiesData.map((story, index) => ( <Image source={{ uri: story.image }} style={styles.storyImage} resizeMode="cover" />

{likes} <Icon name={isBookmarked? 'bookmark' : 'bookmark-o'} size={25} color={isBookmarked? 'black' : '#000'} style={styles.bookmarkIcon} /> <Text numberOfLines={showFullDescription? undefined : 3} style={styles.postDescription}> {post.nickname}: {post.description} {hasEnoughDescriptionLines && !showFullDescription && ( Mostra di più )} {showFullDescription && ( Mostra di meno )} {showComments? ( <> {post.comments.map((comment, index) => ( <Image source={{ uri: comment.image }} style={styles.commentImage} resizeMode="cover" /> {comment.user} {comment.text} ))} Nascondi commenti

</> ) : ( Mostra commenti )} ); }; const PostHeader = ({ post }) => { return ( <Image source={{ uri: post.userImage }} style={styles.profileImage} resizeMode="cover" /> {post.nickname} ); }; const BottomBar = () => { const icons = [ { name: 'home', label: 'Home' }, { name: 'search', label: 'Cerca' }, { name: 'plus-square-o', label: 'Aggiungi' }, { name: 'heart-o', label: 'Attività' }, { name: 'user-o', label: 'Profilo' }, ]; return ( {icons.map((icon, index) => ( ))} ); }; const HomeScreen = () => {

); }; const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#FFF', paddingVertical: 10, }, header: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', paddingHorizontal: 10, marginBottom: 10, }, headerLeft: { flexDirection: 'row', alignItems: 'center', }, headerRight: { paddingRight: 5, }, headerText: { fontSize: 20, fontWeight: 'bold', textAlign: 'center', flex: 1, }, icon: { padding: 5, }, storiesContainer: { flexDirection: 'row', paddingVertical: 10, borderBottomWidth: 1, borderBottomColor: '#EEE', marginBottom: 10, }, story: { alignItems: 'center', marginRight: 15, }, storyImage: { width: 60,

height: 60, borderRadius: 30, }, storyName: { marginTop: 5, }, postContainer: { backgroundColor: '#EEE', marginBottom: 10, padding: 10, }, postImage: { width: '100%', height: 300, }, postInfo: { marginTop: 5, }, postActions: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', }, likesContainer: { flexDirection: 'row', alignItems: 'center', }, likes: { marginLeft: 5, fontWeight: 'bold', }, bookmarkIcon: { padding: 5, }, nickname: { fontWeight: 'bold', }, description: { marginBottom: 5, }, commentsContainer: { marginTop: 5, marginBottom: 10, }, commentContainer: { flexDirection: 'row', alignItems: 'center', marginBottom: 5,

flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', paddingHorizontal: 10, marginBottom: 10, }, profileContainer: { flexDirection: 'row', alignItems: 'center', }, profileImageContainer: { width: 30, height: 30, borderRadius: 15, overflow: 'hidden', marginRight: 10, }, profileImage: { width: '100%', height: '100%', }, profileName: { fontWeight: 'bold', }, }); export default HomeScreen; SEARCH BAR

selected

import React, { useState } from "react"; import { FlatList, SafeAreaView, StatusBar, StyleSheet, Text, TouchableOpacity } from "react-native"; const DATA = [ { id: "1", title: "First Item", }, { id: "2", title: "Second Item", },

id: "3", title: "Third Item", }, ]; const Item = ({ item, onPress, backgroundColor, textColor }) => ( <TouchableOpacity onPress={onPress} style={[styles.item, backgroundColor]}> <Text style={[styles.title, textColor]}>{item.title} ); const Selected = () => { const [selectedId, setSelectedId] = useState(null); const renderItem = ({ item }) => { const backgroundColor = item.id === selectedId? "#6e3b6e" : "#f9c2ff"; const color = item.id === selectedId? 'white' : 'black'; return ( <Item item={item} onPress={() => setSelectedId(item.id)} backgroundColor={{ backgroundColor }} textColor={{ color }} /> ); }; return ( <FlatList data={DATA} renderItem={renderItem} keyExtractor={(item) => item.id} extraData={selectedId} /> ); }; const styles = StyleSheet.create({ container: { flex: 1, marginTop: StatusBar.currentHeight || 0, }, item: { padding: 20,

// Applying filter for the inserted text in search bar const itemData = item.title ? item.title.toUpperCase() : ''.toUpperCase(); const textData = text.toUpperCase(); return itemData.indexOf(textData) > -1; }); setFilteredDataSource(newData); setSearch(text); } else { // Inserted text is blank // Update FilteredDataSource with masterDataSource setFilteredDataSource(masterDataSource); setSearch(text); } }; const ItemView = ({ item }) => { return ( // Flat List Item <Text style={styles.itemStyle} onPress={() => getItem(item)}> {item.id} {'.'} {item.title.toUpperCase()} ); }; const ItemSeparatorView = () => { return ( // Flat List Item Separator <View style={{ height: 0.5, width: '100%', backgroundColor: '#C8C8C8', }} /> ); }; const getItem = (item) => { // Function for click on an item alert('Id : ' + item.id + ' Title : ' + item.title); }; return ( <SafeAreaView style={{ flex: 1}}>

<TextInput style={styles.textInputStyle} onChangeText={(text) => searchFilterFunction(text)} value={search} underlineColorAndroid="transparent" placeholder="Search Here" /> <FlatList data={filteredDataSource} keyExtractor={(item, index) => index.toString()} ItemSeparatorComponent={ItemSeparatorView} renderItem={ItemView} /> ); }; const styles = StyleSheet.create({ container: { backgroundColor: 'white', }, itemStyle: { padding: 10, }, textInputStyle: { height: 40, borderWidth: 1, paddingLeft: 20, margin: 5, borderColor: '#009688', backgroundColor: '#FFFFFF', }, }); export default SearchBarFilter;

SearchBar

import * as React from 'react'; import { Searchbar } from 'react-native-paper'; import { SafeAreaView } from 'react-native'; const SearchBar = () => { const [searchQuery, setSearchQuery] = React.useState('');

<FlatList data={DATA} renderItem={renderItem} keyExtractor={item => item.id} horizontal={true} /> ); } const styles = StyleSheet.create({ container: { flex: 1, marginTop: StatusBar.currentHeight || 0, }, item: { backgroundColor: '#f9c2ff', padding: 20, height : 100, marginVertical: 8, marginHorizontal: 16, }, title: { fontSize: 32, }, }); export default ListHorizontal;

index

import { registerRootComponent } from 'expo'; import App from './App'; import Fetch from './Fetch'; import FetchLike from './FetchLike'; import Heart from './Heart'; import homeButton from './floating/homeButton'; import ListHorizontal from './ListHorizontal'; import SearchBar from './SearchBar'; import Selected from './selected'; import ToDoList from './ToDoList/ToDoList'; // registerRootComponent calls AppRegistry.registerComponent('main', () => App); // It also ensures that whether you load the app in Expo Go or in a native build, // the environment is set up appropriately registerRootComponent(ToDoList);

Heart

import React, {useState, useEffect, useCallback} from "react"; import { View, Text, TouchableOpacity, StyleSheet, SafeAreaView} from "react-native"; import Icon from 'react-native-vector-icons/FontAwesome5'; const Heart = () => { const [isFavorite, setIsFavorite] = useState(false); const tappedHeart = useCallback(() => { setIsFavorite(!isFavorite); }, [isFavorite]); return ( <TouchableOpacity onPress={() => tappedHeart()}> Heart 38 ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor : 'black', justifyContent: 'center', alignItems : 'center' }, /* btnContainer : { flexDirection : 'row', backgroundColor : 'white', marginTop: 20, marginHorizontal : 20, borderRadius : 12, paddingVertical : 10, paddingHorizontal : 20, }, */

const tappedHeart = useCallback((index) => { //console.log(item.id, item.selected); /* setData((prev) => { const newArr = prev; newArr[index].selected = !newArr[index].selected; return newArr; }); / let newArr=[...data]; newArr[index].selected=!newArr[index].selected; setData(newArr); }, [data]); const renderItem = useCallback(({ item, index }) => { //console.log(item.selected); return( <View style={{flexDirection: 'row', alignItems: 'center', padding : 20,}}> <View style = {{marginLeft: 20}}> <TouchableOpacity onPress={() => tappedHeart(index)}> <Icon name="heart" solid={item.selected? true : false} size={26} color='red' /> {item.id}, {item.original_title} ); }, [data]); return ( <View style={{ flex: 1, padding: 24, marginTop : 30 }}> {isLoading? : ( <FlatList data={data} keyExtractor={(item) => item.id} extraData={(item) => item.selected} renderItem={renderItem} /> / <FlatList data={data} keyExtractor={({ id }, index) => id} renderItem={({ item }) => ( {item.title}, {item.releaseYear} )} />

); };

Fetch

import React, { useEffect, useState } from 'react'; import { ActivityIndicator, FlatList, Text, View } from 'react-native'; export default Fetch = () => { const [isLoading, setLoading] = useState(true); const [data, setData] = useState([]); const getMovies = async () => { try { // const response = await fetch('https://reactnative.dev/movies.json'); const response = await fetch("https://api.themoviedb.org/3/tv/94997/season/{season_number}?api_key=a7c4848fcfb 89f8bef0757f282d0a463"); const data = await response.json(); setData(data.episodes); /* const json = await response.json(); setData(json.movies); */ } catch (error) { console.error(error); } finally { setLoading(false); } } useEffect(() => { getMovies(); }, []); return ( <View style={{ flex: 1, padding: 24 }}> {isLoading? : ( <FlatList data={data} keyExtractor={({ id }, index) => id} renderItem={({ item }) => ( {item.id}, {item.name}