Skip to content

Commit 69f16db

Browse files
committed
refactor: simplify QuickStart example
1 parent da69841 commit 69f16db

1 file changed

Lines changed: 14 additions & 65 deletions

File tree

example/src/pages/QuickStart.tsx

Lines changed: 14 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,122 +2,76 @@
22
Rive React Native Quick Start (Nitro)
33
44
Resources:
5-
6-
- Getting Started with Rive + React Native: https://rive.app/docs/runtimes/react-native/react-native
5+
- Getting Started: https://rive.app/docs/runtimes/react-native/react-native
76
- Data Binding: https://rive.app/docs/runtimes/data-binding
8-
- Setting and reading view model properties: https://rive.app/docs/runtimes/data-binding#observability
97
*/
108

11-
import { useEffect, useState } from 'react';
12-
import {
13-
Button,
14-
SafeAreaView,
15-
ScrollView,
16-
StyleSheet,
17-
Text,
18-
} from 'react-native';
9+
import { useEffect } from 'react';
10+
import { Button, SafeAreaView, ScrollView, StyleSheet } from 'react-native';
1911
import {
2012
RiveView,
13+
useRive,
2114
useRiveFile,
2215
useRiveNumber,
2316
useRiveTrigger,
2417
Fit,
2518
DataBindMode,
26-
type ViewModelInstance,
27-
type RiveViewRef,
2819
} from '@rive-app/react-native';
2920
import type { Metadata } from '../helpers/metadata';
3021

3122
export default function QuickStart() {
32-
const { riveFile, error: fileError } = useRiveFile(
23+
const { riveFile } = useRiveFile(
3324
require('../../assets/rive/quick_start.riv')
3425
);
35-
const [riveViewRef, setRiveViewRef] = useState<RiveViewRef | null>(null);
36-
const [viewModelInstance, setViewModelInstance] =
37-
useState<ViewModelInstance | null>(null);
38-
39-
// Get the ViewModelInstance once the RiveView is ready
40-
useEffect(() => {
41-
if (riveViewRef) {
42-
const vmi = riveViewRef.getViewModelInstance();
43-
setViewModelInstance(vmi ?? null);
44-
}
45-
}, [riveViewRef]);
26+
const { riveViewRef, setHybridRef } = useRive();
27+
const viewModelInstance = riveViewRef?.getViewModelInstance();
4628

47-
// This is how you read and set properties from your Rive View Model
4829
const { value: health, setValue: setHealth } = useRiveNumber(
4930
'health',
5031
viewModelInstance
5132
);
5233

53-
// Reference a Trigger from your View Model
5434
const { trigger: gameOverTrigger } = useRiveTrigger(
5535
'gameOver',
5636
viewModelInstance,
57-
{
58-
onTrigger: () => {
59-
// Listen for a Trigger event, whether it comes from the Rive app or from the code
60-
console.log('Game Over Triggered');
61-
},
62-
}
37+
{ onTrigger: () => console.log('Game Over Triggered') }
6338
);
6439

6540
useEffect(() => {
6641
if (viewModelInstance && setHealth) {
67-
// Set the initial health value
6842
setHealth(9);
6943
}
7044
}, [viewModelInstance, setHealth]);
7145

7246
const handleTakeDamage = () => {
7347
if (health !== undefined && setHealth) {
7448
setHealth(health - 7);
75-
// If all state machines have settled, you might need to wake the state machine back up.
76-
// This can happen when all animations have finished playing.
7749
riveViewRef?.play();
7850
}
7951
};
8052

8153
const handleMaxHealth = () => {
82-
if (setHealth) {
83-
setHealth(100);
84-
riveViewRef?.play();
85-
}
54+
setHealth?.(100);
55+
riveViewRef?.play();
8656
};
8757

8858
const handleGameOver = () => {
89-
if (setHealth && gameOverTrigger) {
90-
setHealth(0);
91-
gameOverTrigger();
92-
riveViewRef?.play();
93-
}
59+
setHealth?.(0);
60+
gameOverTrigger?.();
61+
riveViewRef?.play();
9462
};
9563

96-
if (fileError) {
97-
return (
98-
<SafeAreaView style={styles.safeAreaViewContainer}>
99-
<Text style={styles.errorText}>{fileError}</Text>
100-
</SafeAreaView>
101-
);
102-
}
103-
10464
return (
10565
<SafeAreaView style={styles.safeAreaViewContainer}>
10666
<ScrollView contentContainerStyle={styles.container}>
10767
{riveFile && (
10868
<RiveView
109-
hybridRef={{
110-
f: (ref) => setRiveViewRef(ref),
111-
}}
69+
hybridRef={setHybridRef}
11270
file={riveFile}
11371
fit={Fit.Layout}
11472
style={styles.animation}
11573
autoPlay={true}
116-
// DataBindMode.Auto uses the view model instance from your Rive file
11774
dataBind={DataBindMode.Auto}
118-
// Alternative binding options:
119-
// dataBind={{ byName: 'SomeName' }}
120-
// dataBind={DataBindMode.None}
12175
/>
12276
)}
12377
</ScrollView>
@@ -146,9 +100,4 @@ const styles = StyleSheet.create({
146100
width: '100%',
147101
height: 400,
148102
},
149-
errorText: {
150-
color: 'red',
151-
textAlign: 'center',
152-
padding: 20,
153-
},
154103
});

0 commit comments

Comments
 (0)