|
| 1 | +/* |
| 2 | + Rive React Native Quick Start (Nitro) |
| 3 | +
|
| 4 | + Resources: |
| 5 | + - Getting Started: https://rive.app/docs/runtimes/react-native/react-native |
| 6 | + - Data Binding: https://rive.app/docs/runtimes/data-binding |
| 7 | +*/ |
| 8 | + |
| 9 | +import { useEffect, useMemo } from 'react'; |
| 10 | +import { Button, View, StyleSheet } from 'react-native'; |
| 11 | +import { SafeAreaView } from 'react-native-safe-area-context'; |
| 12 | +import { |
| 13 | + RiveView, |
| 14 | + useRive, |
| 15 | + useRiveFile, |
| 16 | + useRiveNumber, |
| 17 | + useRiveTrigger, |
| 18 | + Fit, |
| 19 | + DataBindMode, |
| 20 | +} from '@rive-app/react-native'; |
| 21 | +import type { Metadata } from '../helpers/metadata'; |
| 22 | + |
| 23 | +export default function QuickStart() { |
| 24 | + const { riveFile } = useRiveFile( |
| 25 | + require('../../assets/rive/quick_start.riv') |
| 26 | + ); |
| 27 | + const { riveViewRef, setHybridRef } = useRive(); |
| 28 | + const viewModelInstance = useMemo( |
| 29 | + () => riveViewRef?.getViewModelInstance(), |
| 30 | + [riveViewRef] |
| 31 | + ); |
| 32 | + |
| 33 | + const { value: health, setValue: setHealth } = useRiveNumber( |
| 34 | + 'health', |
| 35 | + viewModelInstance |
| 36 | + ); |
| 37 | + |
| 38 | + const { trigger: gameOverTrigger } = useRiveTrigger( |
| 39 | + 'gameOver', |
| 40 | + viewModelInstance, |
| 41 | + { onTrigger: () => console.log('Game Over Triggered') } |
| 42 | + ); |
| 43 | + |
| 44 | + useEffect(() => { |
| 45 | + setHealth(9); |
| 46 | + }, [setHealth]); |
| 47 | + |
| 48 | + const handleTakeDamage = () => { |
| 49 | + if (health !== undefined) { |
| 50 | + setHealth(health - 7); |
| 51 | + riveViewRef!.play(); |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + const handleMaxHealth = () => { |
| 56 | + setHealth(100); |
| 57 | + riveViewRef!.play(); |
| 58 | + }; |
| 59 | + |
| 60 | + const handleGameOver = () => { |
| 61 | + setHealth(0); |
| 62 | + gameOverTrigger(); |
| 63 | + riveViewRef!.play(); |
| 64 | + }; |
| 65 | + |
| 66 | + return ( |
| 67 | + <SafeAreaView style={styles.safeAreaViewContainer}> |
| 68 | + <View style={styles.container}> |
| 69 | + {riveFile && ( |
| 70 | + <RiveView |
| 71 | + hybridRef={setHybridRef} |
| 72 | + file={riveFile} |
| 73 | + fit={Fit.Layout} |
| 74 | + style={styles.rive} |
| 75 | + autoPlay={true} |
| 76 | + dataBind={DataBindMode.Auto} |
| 77 | + /> |
| 78 | + )} |
| 79 | + </View> |
| 80 | + <Button onPress={handleTakeDamage} title="Take Damage" /> |
| 81 | + <Button onPress={handleMaxHealth} title="Max Health" /> |
| 82 | + <Button onPress={handleGameOver} title="Game Over" /> |
| 83 | + </SafeAreaView> |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 87 | +QuickStart.metadata = { |
| 88 | + name: 'Quick Start', |
| 89 | + description: 'Basic data binding example with health and game over trigger', |
| 90 | +} satisfies Metadata; |
| 91 | + |
| 92 | +const styles = StyleSheet.create({ |
| 93 | + safeAreaViewContainer: { |
| 94 | + flex: 1, |
| 95 | + }, |
| 96 | + container: { |
| 97 | + flex: 1, |
| 98 | + alignItems: 'center', |
| 99 | + justifyContent: 'center', |
| 100 | + }, |
| 101 | + rive: { |
| 102 | + width: '100%', |
| 103 | + height: 400, |
| 104 | + }, |
| 105 | +}); |
0 commit comments