Skip to content

Commit 6052279

Browse files
authored
refactor: streamline QuickStart example with 0.1.2 changes (#93)
- Use `useViewModelInstance(riveViewRef)` instead of manual `useMemo` - Use `onInit` callback for initial health value instead of `useEffect` - Use functional updater `setHealth((h) => (h ?? 0) - 7)` in handleTakeDamage
1 parent 0c18a58 commit 6052279

1 file changed

Lines changed: 19 additions & 36 deletions

File tree

example/src/pages/QuickStart.tsx

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@
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';
11-
import { SafeAreaView } from 'react-native-safe-area-context';
1210
import {
1311
RiveView,
1412
useRive,
1513
useRiveFile,
1614
useRiveNumber,
1715
useRiveTrigger,
16+
useViewModelInstance,
1817
Fit,
19-
DataBindMode,
2018
} from '@rive-app/react-native';
2119
import type { Metadata } from '../helpers/metadata';
2220

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

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

3832
const { trigger: gameOverTrigger } = useRiveTrigger(
3933
'gameOver',
4034
viewModelInstance,
4135
{ onTrigger: () => console.log('Game Over Triggered') }
4236
);
4337

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

5543
const handleMaxHealth = () => {
@@ -64,23 +52,21 @@ export default function QuickStart() {
6452
};
6553

6654
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>
55+
<View style={styles.container}>
56+
{riveFile && viewModelInstance && (
57+
<RiveView
58+
hybridRef={setHybridRef}
59+
file={riveFile}
60+
fit={Fit.Layout}
61+
style={styles.rive}
62+
autoPlay={true}
63+
dataBind={viewModelInstance}
64+
/>
65+
)}
8066
<Button onPress={handleTakeDamage} title="Take Damage" />
8167
<Button onPress={handleMaxHealth} title="Max Health" />
8268
<Button onPress={handleGameOver} title="Game Over" />
83-
</SafeAreaView>
69+
</View>
8470
);
8571
}
8672

@@ -90,9 +76,6 @@ QuickStart.metadata = {
9076
} satisfies Metadata;
9177

9278
const styles = StyleSheet.create({
93-
safeAreaViewContainer: {
94-
flex: 1,
95-
},
9679
container: {
9780
flex: 1,
9881
alignItems: 'center',

0 commit comments

Comments
 (0)