Reference version

This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 56).

滑块

用于从连续或分段范围中选择值的控件。

Android
iOS
Web
Included in Expo Go

For the complete documentation index, see llms.txt. Use this file to discover all available pages.

用于在范围内选择数值的受控滑块。将 valueonValueChange 配对,以从 React 管理状态。

安装

Terminal
npx expo install @expo/ui

If you are installing this in an existing React Native app, make sure to install expo in your project.

用法

连续滑块

默认范围是 [0, 1]

ContinuousSliderExample.tsx
import { useState } from 'react'; import { Host, Slider } from '@expo/ui'; export default function ContinuousSliderExample() { const [value, setValue] = useState(0.5); return ( <Host style={{ flex: 1 }}> <Slider value={value} onValueChange={setValue} /> </Host> ); }

带自定义范围的分段滑块

使用 minmaxstep 来约束滑块生成的值。

SteppedSliderExample.tsx
import { useState } from 'react'; import { Host, Column, Slider, Text } from '@expo/ui'; export default function SteppedSliderExample() { const [volume, setVolume] = useState(50); return ( <Host style={{ flex: 1 }}> <Column spacing={8}> <Text>音量:{volume}</Text> <Slider value={volume} onValueChange={setVolume} min={0} max={100} step={10} /> </Column> </Host> ); }

API

import { Slider } from '@expo/ui';

Component

Slider

Android
iOS
Web

Type: React.Element<SliderProps>

A control for selecting a value from a continuous or stepped range.

Props for the Slider component.

SliderProps

disabled

Android
iOS
Web
Optional • Type: boolean

Whether the slider is disabled. Disabled sliders do not respond to user interaction.

max

Android
iOS
Web
Optional • Type: number • Default: 1

Maximum value of the slider range.

min

Android
iOS
Web
Optional • Type: number • Default: 0

Minimum value of the slider range.

modifiers

Android
iOS
Web
Optional • Type: ModifierConfig[]

Platform-specific modifier escape hatch. Pass an array of modifier configs from @expo/ui/swift-ui/modifiers or @expo/ui/jetpack-compose/modifiers.

onValueChange

Android
iOS
Web
Type: (value: number) => void

Called when the user changes the slider value.

step

Android
iOS
Web
Optional • Type: number

Increment size. For example, step={10} with min={0} and max={100} produces values 0, 10, 20, …, 100.

testID

Android
iOS
Web
Optional • Type: string

Identifier used to locate the component in end-to-end tests.

value

Android
iOS
Web
Type: number

Current value of the slider.