Skip to content
On this page

useCycleList

Category
Last Changed
3 days ago

Cycle through a list of items.

Demo

source

Dog

Usage

import { useCycleList } from '@vueuse/core'

const { state, next, prev } = useCycleList([
  'Dog',
  'Cat',
  'Lizard',
  'Shark',
  'Whale',
  'Dolphin',
  'Octopus',
  'Seal',
])

console.log(state.value) // 'Dog'

prev()

console.log(state.value) // 'Seal'
import { useCycleList } from '@vueuse/core'

const { state, next, prev } = useCycleList([
  'Dog',
  'Cat',
  'Lizard',
  'Shark',
  'Whale',
  'Dolphin',
  'Octopus',
  'Seal',
])

console.log(state.value) // 'Dog'

prev()

console.log(state.value) // 'Seal'

Type Declarations

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: T[],
  options?: UseCycleListOptions<T>
): {
  state: Ref<T>
  index: WritableComputedRef<number>
  next: (n?: number) => T
  prev: (n?: number) => T
}
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: T[],
  options?: UseCycleListOptions<T>
): {
  state: Ref<T>
  index: WritableComputedRef<number>
  next: (n?: number) => T
  prev: (n?: number) => T
}

Source

SourceDemoDocs

Contributors

Anthony Fu
lsdsjy

Changelog

v7.5.5 on 1/25/2022
6d2cb - fix: operator precedence (#1176)
v7.4.0 on 12/18/2021
adceb - feat: new function (#1052)
useCycleList has loaded