React Hooks
"deskset-react" ships a lightweight React runtime built for macOS JavaScriptCore. Import standard React hooks and utilities directly from "deskset-react" with zero external dependencies.
State & Effect Hooks#
useStateHookIn-memory- Local component state returning
[value, setValue]. Resets on app restart — use useStorage when you need disk persistence. useReducerHookState machine- Accepts
(reducer, initialState)and returns[state, dispatch]for multi-action state flows. useRefHookMutable ref- Returns
{ current }persisted across renders without triggering re-renders when mutated. useMemoHookMemoize value- Accepts
(() => value, deps)to cache expensive derived arrays or Canvas drawing ops. useCallbackHookMemoize fn- Accepts
(fn, deps)to keep callback references stable when passing handlers to memoized children. useEffectHookSide effect- Accepts
(() => cleanup?, deps)to run post-commit effects and optionally clean up on unmount. useLayoutEffectHookSync effect- Synchronous variant matching the
useEffectsignature. useContextHookContext read- Reads the current value from a context created with
createContext.
Component Utilities & Constants#
createContextfunctionContext- Creates a shared context object exposing
<MyContext.Provider value={...}>. memofunctionOptimization- Wraps a component
memo(MyRow)to skip re-renders when props are shallow-equal. FragmentComponentGrouping- Returns multiple sibling elements without an extra container (
<>...</>shorthand supported). ChildrenobjectHelpers- Exposes
toArray,map,forEach, andcountfor inspectingprops.children. cloneElementfunctionClone JSX- Clones an existing JSX element while merging extra props.
isValidElementfunctionType guard- Returns
trueif a value is a valid JSX element. API_VERSIONnumberValue 1- Current runtime SDK API level constant for feature detection.
Built-in Global JS APIs#
Standard globals work without imports: fetch, setTimeout / clearTimeout, setInterval / clearInterval, console.log / warn / error, atob / btoa, performance.now(), and ES2020 built-ins (Date, Intl, Map, Set, Promise, URL).
Example: Memoizing Derived Data with useMemo#
TSX
import React, { Chart, Text, VStack, useMemo, useState, useWidget } from "deskset-react";
export default function SmoothTrend() {
const { theme } = useWidget();
const [samples] = useState([12, 18, 15, 27, 24, 31, 29, 36]);
const average = useMemo(() => {
const total = samples.reduce((acc, n) => acc + n, 0);
return (total / samples.length).toFixed(1);
}, [samples]);
return (
<VStack
maxWidth={true}
maxHeight={true}
padding={16}
spacing={8}
background={theme.isPure ? "clear" : "theme.card"}
cornerRadius={theme.isPure ? 0 : theme.corner}
clip={!theme.isPure}
>
<Text fontSize={12} color="theme.secondary">{`7-day avg: ${average}`}</Text>
<Chart kind="line" values={samples} color="theme.accent" height={64} maxWidth={true} />
</VStack>
);
}