RN Extra — Cheatsheet Python → JavaScript / React Native
Variabili e tipi
Section titled “Variabili e tipi”| Python | JavaScript |
|---|---|
x = 1 | const x = 1 o let x = 1 |
x = "ciao" | const x = "ciao" |
x = True | const x = true |
x = None | const x = null |
type(x) | typeof x |
Stringhe
Section titled “Stringhe”| Python | JavaScript |
|---|---|
f"Ciao {nome}" | `Ciao ${nome}` |
s.upper() | s.toUpperCase() |
s.lower() | s.toLowerCase() |
s.strip() | s.trim() |
len(s) | s.length |
Liste / Array
Section titled “Liste / Array”| Python | JavaScript |
|---|---|
lista[0] | arr[0] |
len(lista) | arr.length |
lista.append(x) | arr.push(x) |
lista.pop() | arr.pop() |
x in lista | arr.includes(x) |
lista.sort() | arr.sort() |
Loop e funzioni
Section titled “Loop e funzioni”| Python | JavaScript |
|---|---|
def f(x): | const f = (x) => { } |
def f(): return x | const f = () => x |
lambda x: x*2 | (x) => x * 2 |
for x in lista: | lista.forEach(x => ...) |
[x*2 for x in l] | l.map(x => x * 2) |
[x for x in l if x>2] | l.filter(x => x > 2) |
if x > 0: | if (x > 0) { } |
Oggetti / Dict
Section titled “Oggetti / Dict”| Python | JavaScript |
|---|---|
d = {"a": 1} | const d = {a: 1} |
d["a"] | d.a o d["a"] |
d.get("a") | d?.a (optional chaining) |
{**d1, **d2} | {...o1, ...o2} |
for k, v in d.items(): | Object.entries(d).forEach(([k,v]) => ...) |
React Native — Componenti
Section titled “React Native — Componenti”import { View, Text, Button, TextInput, Image, FlatList, TouchableOpacity, StyleSheet } from 'react-native';import { useState, useEffect } from 'react';import AsyncStorage from '@react-native-async-storage/async-storage';| Componente | Codice |
|---|---|
| View | <View style={...}> |
| Text | <Text style={...}>testo</Text> |
| Button | <Button title="..." onPress={fn} /> |
| TextInput | <TextInput value={x} onChangeText={setX} /> |
| Image | <Image source={{uri: '...'}} style={...} /> |
| FlatList | <FlatList data={d} renderItem={({item})=>...} /> |
| TouchableOpacity | <TouchableOpacity onPress={fn}>... |
| ScrollView | <ScrollView>...</ScrollView> |
const styles = StyleSheet.create({ container: { flex: 1, padding: 20, backgroundColor: '#fff', justifyContent: 'center', alignItems: 'center', },});Stato e effetti
Section titled “Stato e effetti”const [x, setX] = useState(0); // statouseEffect(() => { }, []); // all'avviouseEffect(() => { }, [x]); // quando x cambiaNavigazione (Expo Router)
Section titled “Navigazione (Expo Router)”import { router, useLocalSearchParams, Link } from 'expo-router';
router.push('/path'); // navigarouter.push({ pathname: '/path', params: {id: 1} }); // con parametrirouter.back(); // indietroconst params = useLocalSearchParams(); // leggi parametri<Link href="/path">Vai</Link> // link dichiarativoPersistenza
Section titled “Persistenza”await AsyncStorage.setItem('key', JSON.stringify(dati));const json = await AsyncStorage.getItem('key');const dati = json ? JSON.parse(json) : [];const res = await fetch('https://api.esempio.com/data');const data = await res.json();
const res = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ titolo: '...' }),});