Skip to content
filedom
Esc
↑↓navigate↵open⌘Jpreview
On this page

useSoundTransitions

Plays sounds when observed state changes match rules.

import { useSoundTransitions } from 'filedom/sound';

useSoundTransitions(score, [
  { when: (prev, next) => prev !== undefined && next > prev, sound: 'merge' },
]);

After each change to state, every rule whose when(prev, next) returns true plays its sound. If your state lives in a store that emits domain events, use useEmittedSounds instead.

Parameters

useSoundTransitions<TState>(
  state: TState,
  rules: readonly SoundTransition<TState>[],
  opts?: { playOnMount?: boolean; scheme?: SoundScheme<SoundEvent>; backend?: SoundBackend; }
)

state: value to observe. Rules run when its identity changes, so replace object state instead of mutating it.

rules: each rule has when(prev, next), sound, and optional playback options. sound and options may also be functions of prev and next; a sound function can return null to play nothing. prev is undefined on the first run.

opts: playOnMount (default false) also checks the rules on mount. scheme and backend are optional.

Returns

Nothing (void).

Was this page helpful?