develop

Hooks

The React hooks you know, with the same rules: call them at the top level of a component, in the same order every render.

State and effects

python
from pyxpo import AppState, Text, useEffect, useState

def StateLabel():
    state, setState = useState(AppState.currentState)

    def subscribe():
        sub = AppState.addEventListener("change", setState)
        return sub.remove   # cleanup when the screen goes away

    useEffect(subscribe, [])
    return Text("App is ", state)

Setters accept a value or a function of the old value. An effect returns an optional cleanup function. The deps list works as in React: [] runs once, no list runs after every render.

Async work

use_async runs a coroutine function, or a blocking function in a worker thread, when its deps change. It returns (loading, value, error).

python
import json, urllib.request
from pyxpo import ActivityIndicator, Text, use_async

def load_user():
    with urllib.request.urlopen("https://api.example.com/me") as r:
        return json.load(r)

def Profile():
    loading, user, error = use_async(load_user, [])
    if loading:
        return ActivityIndicator()
    if error:
        return Text("Could not load profile")
    return Text("Hi, ", user["name"])

Context

python
from pyxpo import Text, create_context, use_context

Theme = create_context("light")

def Label():
    theme = use_context(Theme)
    return Text("Theme: ", theme)

def App():
    return Theme.Provider(Label(), value="dark")

All hooks

Every hook has a camelCase name and a snake_case name. They are the same function.

React namePython nameUse it for
useStateuse_stateState that survives renders.
useReduceruse_reducerState updated by a reducer function.
useEffectuse_effectSide effects after render, with cleanup.
useLayoutEffectuse_layout_effectEffects that run before the native side paints.
useRefuse_refA mutable box, or a handle to a native view.
useMemouse_memoCache a computed value until deps change.
useCallbackuse_callbackKeep the same function between renders.
useContextuse_contextRead a value from a Provider above.
useIduse_idA stable unique id.
useImperativeHandleuse_imperative_handleExpose methods through a ref.
useSyncExternalStoreuse_sync_external_storeSubscribe to an outside store.
useTransitionuse_transitionMark an update as not urgent.
useDeferredValueuse_deferred_valueRender a slow value a little later.
-use_asyncRun async or blocking work and track its result.

Theme and layout hooks

React namePython nameUse it for
useAppThemeuse_app_themeThe StyleSheet.configure theme, for values outside styles.
useBreakpointuse_breakpointThe active breakpoint, such as "md".
useBreakpointValueuse_breakpoint_valueThe value of a breakpoint map for this window.
useMediaQueryuse_media_queryTrue when the window matches a CSS-like query.
useWindowDimensionsuse_window_dimensionsWindow width, height, scale and font scale.
useSafeAreaInsetsuse_safe_area_insetsSafe area insets in dp.

See Styling and themes and Responsive layout.

Router hooks such as useLocalSearchParams and useFocusEffect are covered on the Router page.