Quick start

Lay out views, bind controls to state and apply modifiers.

Lay out views

Stacks, text and images work like they do in SwiftUI. Props are the initializer's parameters: VStack(alignment: .leading, spacing: 12) becomes <VStack alignment="leading" spacing={12}>.

import { HStack, Image, Spacer, Text, VStack } from 'swiftui-react-native';

export function Header() {
  return (
    <HStack spacing={12}>
      <Image systemName="sparkles" />
      <VStack alignment="leading" spacing={2}>
        <Text>Release notes</Text>
        <Text>Version 6</Text>
      </VStack>
      <Spacer />
    </HStack>
  );
}

Bind controls to state

A SwiftUI Binding is a value plus a setter. useBinding gives you one, and any prop SwiftUI declares as a Binding takes it.

import { Form, Section, Slider, Toggle, TextField, useBinding } from 'swiftui-react-native';

export function Settings() {
  const wifi = useBinding(true);
  const name = useBinding('');
  const volume = useBinding(0.4);

  return (
    <Form>
      <Section header="Device">
        <Toggle systemImage="wifi" isOn={wifi}>Wi-Fi</Toggle>
        <TextField text={name}>Name</TextField>
        <Slider value={volume} in={[0, 1]} />
      </Section>
    </Form>
  );
}

wifi.value is the current value and wifi.setValue changes it. A boolean binding also has toggle().

Apply modifiers

Modifiers are chained on compose() and passed to any component's modifiers prop. They apply in order, as they do in Swift.

import { Text, compose } from 'swiftui-react-native';

<Text
  modifiers={compose()
    .font('headline')
    .padding(12)
    .background('yellow')
    .clipShape({ rect: { cornerRadius: 10 } })}
>
  Highlighted
</Text>;

More in Composing modifiers.

Repeat content

ForEach takes data and a render function, like SwiftUI's ForEach(_:content:). The elements stay in JavaScript.

import { ForEach, List, Text } from 'swiftui-react-native';

const fruits = [
  { id: 'kiwi', name: 'Kiwi' },
  { id: 'plum', name: 'Plum' },
];

<List>
  <ForEach data={fruits}>{(fruit) => <Text>{fruit.name}</Text>}</ForEach>
</List>;