Composing modifiers

Chain SwiftUI modifiers with compose() and pass them to any component.

compose()

compose() starts an empty modifier chain. Each method adds one SwiftUI modifier and returns the chain, so modifiers read the way they do in Swift:

<Text modifiers={compose().padding().background('blue').foregroundStyle('white')}>
  Hello
</Text>
Text("Hello")
    .padding()
    .background(.blue)
    .foregroundStyle(.white)

Order matters, exactly as in SwiftUI: padding before a background makes the background larger, and padding after it adds space outside.

Common view props

Every component accepts these props, on top of its initializer's props:

PropTypeMeaning
modifierscompose() chainSwiftUI modifiers, applied in order
styleStyleProp<ViewStyle>React Native layout for the component's box (Yoga)
testIDstringTest identifier

Overloads

Many modifiers have several SwiftUI overloads. Each has its own React form, and the one that runs is chosen by the arguments you pass:

compose().padding();                       // padding(_:_:) with defaults
compose().padding(12);                     // padding(_:)
compose().padding('horizontal', 20);       // edges and length
compose().padding({ top: 8, leading: 16, bottom: 8, trailing: 16 }); // EdgeInsets

Modifiers that take several labeled parameters take one options object instead, named after the Swift labels:

compose().sheet({
  isPresented: showSettings,
  onDismiss: () => save(),
  content: <SettingsView />,
});

Content in modifiers

When a SwiftUI modifier takes a view builder, such as overlay, background with content, toolbar or sheet, you pass React content. It renders as SwiftUI views:

compose().overlay({ alignment: 'topTrailing', content: <Badge count={3} /> });

Animations

withAnimation works as in Swift. State changes inside the closure animate every SwiftUI view they affect.

import { withAnimation } from 'swiftui-react-native';

<Button onPress={() => withAnimation('snappy', () => setCount((count) => count + 1))}>
  Add
</Button>;

Browse every modifier in the modifier reference.