Skip to content

useCycleList

Category
Export Size
444 B
Last Changed
5 months ago

Cycle through a list of items.

Learn how to use useCycleList to create an image carousel with this FREE video lesson from Vue School!

Demo

Usage

ts
import { 
useCycleList
} from '@vueuse/core'
const {
state
,
next
,
prev
,
go
} =
useCycleList
([
'Dog', 'Cat', 'Lizard', 'Shark', 'Whale', 'Dolphin', 'Octopus', 'Seal', ])
console
.
log
(
state
.
value
) // 'Dog'
prev
()
console
.
log
(
state
.
value
) // 'Seal'
go
(3)
console
.
log
(
state
.
value
) // 'Shark'

Type Declarations

ts
export interface 
UseCycleListOptions
<
T
> {
/** * The initial value of the state. * A ref can be provided to reuse. */
initialValue
?:
MaybeRef
<
T
>
/** * The default index when */
fallbackIndex
?: number
/** * Custom function to get the index of the current value. */
getIndexOf
?: (
value
:
T
,
list
:
T
[]) => number
} /** * Cycle through a list of items * * @see https://vueuse.org/useCycleList */ export declare function
useCycleList
<
T
>(
list
:
MaybeRefOrGetter
<
T
[]>,
options
?:
UseCycleListOptions
<
T
>,
):
UseCycleListReturn
<
T
>
export interface
UseCycleListReturn
<
T
> {
state
:
ShallowRef
<
T
>
index
:
WritableComputedRef
<number>
next
: (
n
?: number) =>
T
prev
: (
n
?: number) =>
T
/** * Go to a specific index */
go
: (
i
: number) =>
T
}

Source

SourceDemoDocs

Contributors

Anthony Fu
IlyaL
Anthony Fu
Poet
xiaofan
Helio S. Junior
Waleed Khaled
Jelf
markthree
lsdsjy

Changelog

v12.8.0 on
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
v12.3.0 on
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.8.0 on
2ae36 - feat: add go function (#3615)
v10.1.0 on
659b2 - fix: correctly wrap list with ref (#2988)
v10.0.0-beta.4 on
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
10e98 - feat(toRef)!: rename resolveRef to toRef
0a72b - feat(toValue): rename resolveUnref to toValue

Released under the MIT License.

AI-Driven Development
You. But 10x faster
The structured masterclass to help you leverage AI and build with an edge
Join Early