Stopwatch Examples Overview
Stopwatch Examples
Section titled “Stopwatch Examples”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.
Overview of Examples
Section titled “Overview of Examples”Comparison of Approaches
Section titled “Comparison of Approaches”State Management Approaches
Section titled “State Management Approaches”The stopwatch examples demonstrate three main approaches to managing state:
-
Internal Machine State: Using the state machine’s internal data to track elapsed time
- Basic Stopwatch - Foundational approach with lifecycle hooks
- Stopwatch with Data and Hooks - Clean implementation with when helper
- Stopwatch with Transition Functions - Enhanced type safety with transition functions
-
React State: Using React’s useState to track elapsed time, with the machine managing only the mode
- Stopwatch with React State and Effects - React-first approach with useEffect
- Stopwatch with State Effects - Specialized effect hooks for cleaner code
-
External State: Managing elapsed time in external React state that’s synchronized with the machine
- Stopwatch with External React State - Demonstrates state management outside the machine
- Stopwatch with Lifecycle - Declarative lifecycle API instead of useEffect
Effect Management Approaches
Section titled “Effect Management Approaches”The examples also showcase different approaches to handling side effects (especially the timer):
- Lifecycle Hooks: Using
enter
/exit
hooks to manage the timer (stopwatch
) - When Helper: Using the
when
helper for conditional effects (stopwatch-using-data-and-hooks
) - React useEffect: Using React’s useEffect with state dependencies (
stopwatch-using-react-state-and-effects
) - Specialized Effect Hooks: Using
useStateEffects
anduseEventTypeEffect
(stopwatch-using-react-state-and-state-effects
,stopwatch-using-external-react-state-and-state-effects
) - onLifecycle API: Using the declarative
onLifecycle
API (stopwatch-using-react-state-using-lifecycle-instead-of-useeffect
)
Implementation Differences
Section titled “Implementation Differences”Example | State Management | Effect Management | Type Safety | Code Complexity |
---|---|---|---|---|
Basic Stopwatch | Machine Data | Lifecycle Hooks | Good | Medium |
Data and Hooks | Machine Data | When Helper | Good | Low |
Transition Functions | Machine Data | When Helper | Excellent | Medium |
React State and Effects | React useState | React useEffect | Good | Low |
State Effects | React useState | Effect Hooks | Good | Low |
External React State | External useState | Effect Hooks | Good | Medium |
Lifecycle | React useState | onLifecycle API | Excellent | Medium |
Recommended Example
Section titled “Recommended Example”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.
Redundant Examples?
Section titled “Redundant Examples?”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:
- Stopwatch with Data and Hooks - Clean internal state approach
- Stopwatch with Transition Functions - Best type safety
- Stopwatch with State Effects - React integration
- Stopwatch with Lifecycle - Declarative lifecycle API