Skip to content

Commit 6b30587

Browse files
committed
refactor: streamline QuickStart example with useViewModelInstance and functional updater
1 parent 0c18a58 commit 6b30587

1 file changed

Lines changed: 9 additions & 20 deletions

File tree

example/src/pages/QuickStart.tsx

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
- Data Binding: https://rive.app/docs/runtimes/data-binding
77
*/
88

9-
import { useEffect, useMemo } from 'react';
109
import { Button, View, StyleSheet } from 'react-native';
1110
import { SafeAreaView } from 'react-native-safe-area-context';
1211
import {
@@ -15,8 +14,8 @@ import {
1514
useRiveFile,
1615
useRiveNumber,
1716
useRiveTrigger,
17+
useViewModelInstance,
1818
Fit,
19-
DataBindMode,
2019
} from '@rive-app/react-native';
2120
import type { Metadata } from '../helpers/metadata';
2221

@@ -25,31 +24,21 @@ export default function QuickStart() {
2524
require('../../assets/rive/quick_start.riv')
2625
);
2726
const { riveViewRef, setHybridRef } = useRive();
28-
const viewModelInstance = useMemo(
29-
() => riveViewRef?.getViewModelInstance(),
30-
[riveViewRef]
31-
);
27+
const viewModelInstance = useViewModelInstance(riveFile, {
28+
onInit: (vmi) => (vmi.numberProperty('health')!.value = 9),
29+
});
3230

33-
const { value: health, setValue: setHealth } = useRiveNumber(
34-
'health',
35-
viewModelInstance
36-
);
31+
const { setValue: setHealth } = useRiveNumber('health', viewModelInstance);
3732

3833
const { trigger: gameOverTrigger } = useRiveTrigger(
3934
'gameOver',
4035
viewModelInstance,
4136
{ onTrigger: () => console.log('Game Over Triggered') }
4237
);
4338

44-
useEffect(() => {
45-
setHealth(9);
46-
}, [setHealth]);
47-
4839
const handleTakeDamage = () => {
49-
if (health !== undefined) {
50-
setHealth(health - 7);
51-
riveViewRef!.play();
52-
}
40+
setHealth((h) => (h ?? 0) - 7);
41+
riveViewRef!.play();
5342
};
5443

5544
const handleMaxHealth = () => {
@@ -66,14 +55,14 @@ export default function QuickStart() {
6655
return (
6756
<SafeAreaView style={styles.safeAreaViewContainer}>
6857
<View style={styles.container}>
69-
{riveFile && (
58+
{riveFile && viewModelInstance && (
7059
<RiveView
7160
hybridRef={setHybridRef}
7261
file={riveFile}
7362
fit={Fit.Layout}
7463
style={styles.rive}
7564
autoPlay={true}
76-
dataBind={DataBindMode.Auto}
65+
dataBind={viewModelInstance}
7766
/>
7867
)}
7968
</View>

0 commit comments

Comments
 (0)