react-quizlet-flashcard

useFlashcardArray Hook

Manages state and navigation for a deck of flashcards in the react-quizlet-flashcard library. Integrates with the FlashcardArray component to handle card navigation, flipping, and display options.

Usage

Controls a deck of flashcards, providing navigation, progress tracking, and flip state management. Use with FlashcardArray or custom implementations.

You can import the useFlashcardArray hook from the library:

import { useFlashcardArray } from 'react-quizlet-flashcard';

Basic Example

import { useFlashcardArray, FlashcardArray } from 'react-quizlet-flashcard';
import 'react-quizlet-flashcard/dist/index.css'

const deck = [
  { id: 1, front: { html: <div>Front 1</div> }, back: { html: <div>Back 1</div> } },
  { id: 2, front: { html: <div>Front 2</div> }, back: { html: <div>Back 2</div> } },
];

const MyFlashcardArray = () => {
  const flipArrayHook = useFlashcardArray({ deckLength: deck.length });
  return <FlashcardArray flipArrayHook={flipArrayHook} deck={deck} />;
};

Hook API

Input Parameters

ParameterTypeDefaultDescription
deckLengthnumberRequiredTotal number of cards in the deck.
cycle?booleanfalseEnables cyclic navigation (loops to first card after last).
showCount?booleantrueShows current card number and total (e.g., "1/5").
showControls?booleantrueShows previous/next navigation buttons.
showProgressBar?booleanfalseShows a progress bar for deck position.
onCardChange?(cardIndex: number) => voidundefinedCallback when the current card changes, receiving the new index.
onFlip?(cardIndex: number, state: FlipState) => voidundefinedCallback when the current card flips, receiving index and FlipState.
manualFlip?booleanfalseDisables automatic flipping on click for the active card.
disableFlip?booleanfalseDisables flipping for the active card.
flipDirection?FlipDirection'bt'Flip animation direction ('rtl', 'ltr', 'tb', 'bt').

Return Value

Since useFlashcardArray hook is also used internally by the FlashcardArray component, it returns a UseFlashcardArray object with the following properties and methods. Most of these properties are not very useful outside of the FlashcardArray component, but they are available for advanced use cases:

Property/MethodTypeDescription
cyclebooleanReflects cycle input.
showCountbooleanReflects showCount input.
showControlsbooleanReflects showControls input.
showProgressBarbooleanReflects showProgressBar input.
deckLengthnumberTotal number of cards.
currentCardnumberIndex of the current card (0-based).
cardsInDisplaynumber[]Indices of left, center, and right cards (e.g., [-1, 0, 1] for non-cyclic).
flipHookUseFlashcardHook managing the active card's flip state.
progressBar{ current: number, total: number, percentage: number }Progress info (current card, total, and percentage).
prevCard() => voidNavigates to the previous card.
nextCard() => voidNavigates to the next card.
setCurrentCard(index: number) => voidSets the current card to the specified index.

Types

  • FlipState: Enum for card state (FlipState.Front, FlipState.Back).
  • FlipDirection: Flip animation direction ('rtl', 'ltr', 'tb', 'bt').

Examples

For examples, check out the Use-cases section, which includes various configurations and scenarios for using the useFlashcardArray hook.

Best Practices

  • Type Safety: Use TypeScript to leverage type checking for inputs and outputs.
  • Stable Props: Pass stable references for onCardChange and onFlip to avoid unnecessary re-renders.
  • Accessibility: Ensure FlashcardArray uses ARIA attributes for screen reader support.
  • Responsive Design: Test with the component's responsive styles (90% width at 625px).