develop

Components and APIs

Components have the same names and props as React Native. Children are positional arguments. Props are keyword arguments.

Writing components

A component is a function that returns elements. Pass children first, then props. Both camelCase and snake_case prop names work.

python
from pyxpo import Image, Pressable, StyleSheet, Text, View

def Card(title, image, on_open):
    return Pressable(
        View(
            Image(source={"uri": image}, style=styles.image),
            Text(title, style=styles.title),
        ),
        onPress=on_open,
        style=styles.card,
    )

styles = StyleSheet.create(
    card=dict(borderRadius=16, backgroundColor="#fff", overflow="hidden"),
    image=dict(height=160),
    title=dict(padding=12, fontSize=17, fontWeight="600"),
)

onPress and on_press are the same prop. Passing both raises an error, like a duplicate keyword argument.

Lists

FlatList renders long lists and only builds the rows on screen.

python
from pyxpo import FlatList, Text

def Names(people):
    return FlatList(
        data=people,
        keyExtractor=lambda p, i: p["id"],
        renderItem=lambda row: Text(row["item"]["name"]),
    )

Styles

Styles are dicts with React Native keys: flexbox layout, colors, borders, shadows and transforms. Use StyleSheet.create to name them, and pass a list to combine several. Light and dark themes, dynamic styles and breakpoint values are on Styling and themes.

python
Text("Hi", style=[styles.title, done and styles.done])

Core components

  • View
  • Text
  • Image
  • ScrollView
  • FlatList
  • SectionList
  • TextInput
  • Button
  • Pressable
  • TouchableOpacity
  • TouchableHighlight
  • Switch
  • Slider
  • Picker
  • Modal
  • ActivityIndicator
  • RefreshControl
  • SafeAreaView
  • KeyboardAvoidingView
  • StatusBar
  • ErrorBoundary

Layout components

Responsive building blocks on top of View. See Responsive layout.

  • Container
  • Row
  • Col
  • Grid
  • GridItem
  • Flex
  • HStack
  • VStack
  • Center
  • Spacer
  • Show
  • Hide

APIs

  • Alert
  • Appearance
  • AppState
  • BackHandler
  • Clipboard
  • Constants
  • Dimensions
  • I18nManager
  • Keyboard
  • Linking
  • PanResponder
  • PixelRatio
  • Platform
  • SplashScreen
  • StyleSheet
  • ToastAndroid
  • Vibration
python
from pyxpo import Alert, Linking, Platform

def confirm_delete(on_yes):
    Alert.alert("Delete note?", "This cannot be undone.", [
        {"text": "Cancel", "style": "cancel"},
        {"text": "Delete", "style": "destructive", "onPress": on_yes},
    ])

if Platform.OS == "ios":
    Linking.openURL("https://example.com")

More modules in pyxpo

ModuleUse it for
pyxpo.animatedAnimated values, timing and spring animations.
pyxpo.gesture_handlerPan, tap and other gestures.
pyxpo.safe_areaSafe area insets and SafeAreaProvider.
pyxpo.vector_iconsIcon fonts for buttons and tabs.
pyxpo.fontsLoad custom fonts from assets.