|
7 | 7 |
|
8 | 8 | feat!: introduce hooks-based API for v2.0 |
9 | 9 |
|
10 | | -BREAKING CHANGE: Complete API redesign with React hooks |
| 10 | +> [!important] |
| 11 | +> BREAKING CHANGE: **Complete API Redesign with React Hooks** |
11 | 12 |
|
12 | | -- Replace `YoutubePlayer` component with `YoutubeView` + `useYouTubePlayer` hook |
13 | | -- Add `useYouTubeEvent` hook for reactive event handling |
14 | | -- Remove ref-based imperative API in favor of declarative approach |
15 | | -- Simplify component props and reduce coupling between components |
16 | | -- Follow expo patterns for better DX |
| 13 | +#### New Hooks-Based Architecture |
17 | 14 |
|
18 | | -Migration required from v1: |
| 15 | +- **`useYouTubePlayer(videoId, config)`** - Creates a player instance with declarative configuration |
| 16 | +- **`useYouTubeEvent(player, eventName, intervalOrDefault?, deps?)`** - Reactive event handling with complete type inference |
| 17 | +- **`YoutubeView`** - Simplified view component that accepts a player instance |
19 | 18 |
|
20 | | -- `YoutubePlayer` → `YoutubeView` + `useYouTubePlayer()` |
21 | | -- Event props → `useYouTubeEvent()` hooks |
22 | | -- `playerRef.current.method()` → `player.method()` |
| 19 | +#### Migration from v1 to v2 |
23 | 20 |
|
24 | | -Fixes: Memory leaks, complex state management, tight coupling |
25 | | -Improves: Developer experience, maintainability, performance |
| 21 | +**Before (v1):** |
| 22 | +```jsx |
| 23 | +// Imperative, ref-based API |
| 24 | +const playerRef = useRef<PlayerControls>(null); |
| 25 | + |
| 26 | +<YoutubePlayer |
| 27 | + ref={playerRef} |
| 28 | + source={videoId} |
| 29 | + onReady={handleReady} |
| 30 | + onStateChange={handleStateChange} |
| 31 | + onProgress={handleProgress} |
| 32 | + playerVars={{ autoplay: true }} |
| 33 | +/> |
| 34 | + |
| 35 | +// Manual event handlers and state management |
| 36 | +const [isPlaying, setIsPlaying] = useState(false); |
| 37 | +const handleStateChange = (state) => setIsPlaying(state === PlayerState.PLAYING); |
| 38 | +``` |
| 39 | + |
| 40 | +**After (v2):** |
| 41 | +```jsx |
| 42 | +// Declarative, hooks-based API |
| 43 | +const player = useYouTubePlayer(videoId, { |
| 44 | + autoplay: true, |
| 45 | + controls: true, |
| 46 | + playsinline: true |
| 47 | +}); |
| 48 | + |
| 49 | +<YoutubeView player={player} /> |
| 50 | + |
| 51 | +// Reactive state with automatic updates - two usage patterns: |
| 52 | + |
| 53 | +// 1. State-based (returns reactive values) |
| 54 | +const playbackRate = useYouTubeEvent(player, 'playbackRateChange', 1); |
| 55 | +const progress = useYouTubeEvent(player, 'progress', 1000); // 1000ms interval |
| 56 | +const state = useYouTubeEvent(player, 'stateChange'); |
| 57 | + |
| 58 | +const isPlaying = state === PlayerState.PLAYING; |
| 59 | + |
| 60 | +// 2. Callback-based (for side effects) |
| 61 | +useYouTubeEvent(player, 'ready', (playerInfo) => { |
| 62 | + console.log('Player ready:', playerInfo); |
| 63 | +}); |
| 64 | + |
| 65 | +useYouTubeEvent(player, 'error', (error) => { |
| 66 | + console.error('Player error:', error); |
| 67 | +}); |
| 68 | +``` |
| 69 | + |
| 70 | +### ✨ New Features |
| 71 | + |
| 72 | +- **Declarative Configuration**: Configure player options directly in `useYouTubePlayer` hook |
| 73 | +- **Automatic State Management**: No need to manually manage state for common use cases |
| 74 | +- **Reactive Events**: `useYouTubeEvent` with two usage patterns - state-based for reactive values and callback-based for side effects |
| 75 | +- **Better TypeScript Support**: Improved type inference and autocomplete |
| 76 | +- **Reduced Boilerplate**: Significantly less code required for common operations |
| 77 | +- **Automatic Cleanup**: Hooks handle cleanup automatically, preventing memory leaks |
| 78 | + |
| 79 | +### 🎯 Improvements |
| 80 | + |
| 81 | +- **Reduced Coupling**: Eliminated ref dependencies between parent and child components |
| 82 | +- **Simplified API**: Fewer props to manage, more intuitive usage patterns |
| 83 | +- **Better Developer Experience**: Following established React Native patterns (expo-audio, expo-video) |
| 84 | +- **Performance**: More efficient event handling with automatic cleanup |
| 85 | +- **Maintainability**: Cleaner separation of concerns |
| 86 | + |
| 87 | +### 📦 Component Changes |
| 88 | + |
| 89 | +#### Removed |
| 90 | +- ❌ `YoutubePlayer` component (replaced by `YoutubeView`) |
| 91 | +- ❌ `PlayerControls` ref interface |
| 92 | +- ❌ Direct event props (`onReady`, `onStateChange`, `onProgress`, etc.) |
| 93 | + |
| 94 | +#### Added |
| 95 | +- ✅ `YoutubeView` component |
| 96 | +- ✅ `useYouTubePlayer` hook |
| 97 | +- ✅ `useYouTubeEvent` hook |
| 98 | +- ✅ Simplified prop interface |
| 99 | + |
| 100 | +### 📚 Migration Guide |
| 101 | + |
| 102 | +For detailed migration examples and step-by-step instructions, see our [Migration Guide](/packages/react-native-youtube-bridge/docs/migration-v2.md). |
| 103 | + |
| 104 | +Key migration steps: |
| 105 | +1. **Replace `YoutubePlayer` with `YoutubeView` + `useYouTubePlayer`** |
| 106 | +2. **Convert event props to `useYouTubeEvent` hooks** (state-based or callback-based) |
| 107 | +3. **Move `playerVars` to `useYouTubePlayer` config** |
| 108 | +4. **Replace ref-based method calls with direct player method calls** |
| 109 | +5. **Remove manual state management for events** |
| 110 | + |
| 111 | +### ⚠️ Breaking Changes |
| 112 | + |
| 113 | +- **API Surface**: Complete API redesign, no backward compatibility |
| 114 | +- **Event Handling**: Manual event listeners replaced with reactive hooks |
| 115 | +- **Component Structure**: `YoutubePlayer` split into `YoutubeView` + hooks |
| 116 | + |
| 117 | +### 🐛 Bug Fixes |
| 118 | +- Fixed memory leaks from improper event cleanup |
| 119 | +- Better handling of rapid video ID changes |
| 120 | +- Manage multiple players independently |
| 121 | + |
| 122 | +### 📖 Documentation |
| 123 | +- Complete API documentation rewrite |
| 124 | +- Added migration guide from v1 |
| 125 | +- Updated all examples to use v2 API |
| 126 | +- Added TypeScript usage examples |
| 127 | + |
| 128 | +--- |
| 129 | + |
| 130 | +## Previous Versions |
| 131 | + |
| 132 | +### [1.x.x] - Legacy Version |
| 133 | +See [v1 documentation](/packages/react-native-youtube-bridge/docs/v1.md) for the previous imperative API. |
0 commit comments