Skip to main content

Lesson 4 of 9

Native Form Controls - Picker, Switch, Slider

Mobile forms use native controls that feel right for each platform. ActionSheet on iOS vs Dialog on Android, native date pickers, switches, and sliders all have React Native equivalents.

20 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Switch, Slider, and ActionSheet

Switch, Slider, and ActionSheet
import { Switch, View, Text, Alert, StyleSheet } from 'react-native';
// npm install @react-native-community/slider @react-native-community/datetimepicker

// ---- Switch ----
function SettingRow({ label, value, onValueChange }: { label: string; value: boolean; onValueChange: (v: boolean) => void }) {
  return (
    <View style={styles.settingRow}>
      <Text style={styles.settingLabel}>{label}</Text>
      <Switch
        value={value}
        onValueChange={onValueChange}
        trackColor={{ false: '#334155', true: '#229c6b' }}
        thumbColor={value ? '#fff' : '#94a3b8'}
        ios_backgroundColor="#334155"
        accessibilityRole="switch"
        accessibilityLabel={label}
        accessibilityState={{ checked: value }}
      />
    </View>
  );
}

// ---- Slider ----
import Slider from '@react-native-community/slider';
function VolumeControl({ value, onChange }: { value: number; onChange: (v: number) => void }) {
  return (
    <View>
      <Text style={styles.settingLabel}>Volume: {Math.round(value * 100)}%</Text>
      <Slider
        value={value}
        onValueChange={onChange}
        minimumValue={0}
        maximumValue={1}
        step={0.01}
        minimumTrackTintColor="#229c6b"
        maximumTrackTintColor="#334155"
        thumbTintColor="#229c6b"
        accessibilityLabel="Volume slider"
      />
    </View>
  );
}

// ---- ActionSheet (platform-native options menu) ----
function showOptions() {
  Alert.alert(
    'Export Format',
    'Choose export format',
    [
      { text: 'PDF', onPress: () => exportAs('pdf') },
      { text: 'CSV', onPress: () => exportAs('csv') },
      { text: 'JSON', onPress: () => exportAs('json') },
      { text: 'Cancel', style: 'cancel' },
    ],
    { cancelable: true }
  );
  // On iOS: ActionSheet appears from bottom
  // On Android: Alert dialog appears in center
}

// For full native ActionSheet on iOS, use @expo/react-native-action-sheet
import { useActionSheet } from '@expo/react-native-action-sheet';
function ExportButton() {
  const { showActionSheetWithOptions } = useActionSheet();
  const options = ['PDF', 'CSV', 'JSON', 'Cancel'];

  function onPress() {
    showActionSheetWithOptions(
      { options, cancelButtonIndex: 3, destructiveButtonIndex: undefined },
      (selectedIndex) => {
        if (selectedIndex !== undefined && selectedIndex < 3) {
          exportAs(options[selectedIndex].toLowerCase() as 'pdf' | 'csv' | 'json');
        }
      }
    );
  }
  return <Pressable onPress={onPress}><Text>Export</Text></Pressable>;
}

function exportAs(format: 'pdf' | 'csv' | 'json') {
  console.log('Exporting as', format);
}

const styles = StyleSheet.create({
  settingRow: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', paddingVertical: 14, paddingHorizontal: 16, backgroundColor: '#1e293b', borderRadius: 10, marginBottom: 8 },
  settingLabel: { fontSize: 15, color: '#f1f5f9' },
});

Tip

Tip

Practice Native Form Controls Picker Switch Slider in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Integrating cross-platform switches, sliders, and modal selection pickers

Practice Task

Note

Practice Task - (1) Write a working example of Native Form Controls Picker Switch Slider from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Native Form Controls Picker Switch Slider is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.

Topics in This Module

0/9 done