Skip to content

Stopwatch Examples Overview

The stopwatch examples demonstrate various approaches to implementing a common UI pattern - a stopwatch with start, stop, pause, and reset functionality. Each example showcases different Matchina features and React integration patterns.

These examples provide a progression from simple to advanced implementations, each highlighting distinct aspects of Matchina’s capabilities and React integration patterns.

The stopwatch examples demonstrate three main approaches to managing state:

  1. Internal Machine State: Using the state machine’s internal data to track elapsed time

  2. React State: Using React’s useState to track elapsed time, with the machine managing only the mode

  3. External State: Managing elapsed time in external React state that’s synchronized with the machine

The examples also showcase different approaches to handling side effects (especially the timer):

  1. Lifecycle Hooks: Using enter/exit hooks to manage the timer (stopwatch)
  2. When Helper: Using the when helper for conditional effects (stopwatch-using-data-and-hooks)
  3. React useEffect: Using React’s useEffect with state dependencies (stopwatch-using-react-state-and-effects)
  4. Specialized Effect Hooks: Using useStateEffects and useEventTypeEffect (stopwatch-using-react-state-and-state-effects, stopwatch-using-external-react-state-and-state-effects)
  5. onLifecycle API: Using the declarative onLifecycle API (stopwatch-using-react-state-using-lifecycle-instead-of-useeffect)
ExampleState ManagementEffect ManagementType SafetyCode Complexity
Basic StopwatchMachine DataLifecycle HooksGoodMedium
Data and HooksMachine DataWhen HelperGoodLow
Transition FunctionsMachine DataWhen HelperExcellentMedium
React State and EffectsReact useStateReact useEffectGoodLow
State EffectsReact useStateEffect HooksGoodLow
External React StateExternal useStateEffect HooksGoodMedium
LifecycleReact useStateonLifecycle APIExcellentMedium

If you’re new to Matchina, we recommend starting with the Stopwatch with Data and Hooks example. It provides a clean implementation with good type safety and minimal complexity.

For more advanced type safety, explore the Stopwatch with Transition Functions example.

For a React-focused approach, try the Stopwatch with State Effects example, which provides the cleanest integration with React’s state management.

While we’ve provided multiple implementations to showcase different patterns, you might wonder which ones provide truly unique value:

  • The Basic Stopwatch and Stopwatch with Data and Hooks are similar, but the latter provides a cleaner implementation with the “when” helper.
  • The React State and Effects and State Effects examples are similar, but the latter uses specialized hooks for cleaner code.
  • The External React State and Lifecycle examples are similar, but the latter demonstrates the declarative lifecycle API.

Each example has been carefully crafted to demonstrate a specific integration pattern or Matchina feature, but if you’re looking for the most distinct implementations, focus on:

  1. Stopwatch with Data and Hooks - Clean internal state approach
  2. Stopwatch with Transition Functions - Best type safety
  3. Stopwatch with State Effects - React integration
  4. Stopwatch with Lifecycle - Declarative lifecycle API