Skip to content

Commit d18b42a

Browse files
committed
refactor: simplify MenuListExample with DataBindMode.Auto + useViewModelInstance(riveViewRef)
1 parent 2006ca5 commit d18b42a

1 file changed

Lines changed: 29 additions & 32 deletions

File tree

example/src/pages/MenuListExample.tsx

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import {
1616
useRiveFile,
1717
useRiveList,
1818
useViewModelInstance,
19+
useRive,
20+
DataBindMode,
1921
} from '@rive-app/react-native';
2022
import { type Metadata } from '../helpers/metadata';
2123

@@ -29,36 +31,18 @@ export default function MenuListExample() {
2931
{isLoading ? (
3032
<ActivityIndicator size="large" color="#007AFF" />
3133
) : riveFile ? (
32-
<WithViewModelSetup file={riveFile} />
34+
<MenuList file={riveFile} />
3335
) : (
3436
<Text style={styles.errorText}>{error || 'Unexpected error'}</Text>
3537
)}
3638
</View>
3739
);
3840
}
3941

40-
function WithViewModelSetup({ file }: { file: RiveFile }) {
41-
const viewModel = useMemo(() => file.viewModelByName('main'), [file]);
42-
const instance = useViewModelInstance(viewModel);
42+
function MenuList({ file }: { file: RiveFile }) {
43+
const { riveViewRef, setHybridRef } = useRive();
44+
const instance = useViewModelInstance(riveViewRef);
4345

44-
if (!instance) {
45-
return (
46-
<Text style={styles.errorText}>
47-
Failed to create view model instance for 'main'
48-
</Text>
49-
);
50-
}
51-
52-
return <MenuList instance={instance} file={file} />;
53-
}
54-
55-
function MenuList({
56-
instance,
57-
file,
58-
}: {
59-
instance: ViewModelInstance;
60-
file: RiveFile;
61-
}) {
6246
const addLabelRef = useRef<TextInput>(null);
6347
const lastAdded = useRef<ViewModelInstance | null>(null);
6448
const indexToDeleteRef = useRef<TextInput>(null);
@@ -134,22 +118,24 @@ function MenuList({
134118
menuItemLabel.value = label;
135119
};
136120

137-
if (error) {
138-
return <Text style={styles.errorText}>{error.message}</Text>;
139-
}
121+
const controlsDisabled = !instance;
140122

141123
return (
142124
<View style={styles.container}>
143125
<RiveView
126+
hybridRef={setHybridRef}
144127
style={styles.rive}
145128
autoPlay={true}
146-
dataBind={instance}
129+
dataBind={DataBindMode.Auto}
147130
fit={Fit.FitWidth}
148131
file={file}
149132
/>
150133

151134
<ScrollView style={styles.controls}>
152-
<Text style={styles.listLength}>Menu Items: {length}</Text>
135+
<Text style={styles.listLength}>
136+
{instance ? `Menu Items: ${length}` : 'Loading...'}
137+
</Text>
138+
{error && <Text style={styles.errorText}>{error.message}</Text>}
153139

154140
<View style={styles.controlGroup}>
155141
<TextInput
@@ -160,12 +146,17 @@ function MenuList({
160146
onChangeText={(text) => (addLabelValue.current = text)}
161147
/>
162148
<TouchableOpacity
163-
style={styles.button}
149+
style={[styles.button, controlsDisabled && styles.buttonDisabled]}
164150
onPress={() => addNewMenuItem(addLabelValue.current)}
151+
disabled={controlsDisabled}
165152
>
166153
<Text style={styles.buttonText}>Add Menu Item</Text>
167154
</TouchableOpacity>
168-
<TouchableOpacity style={styles.button} onPress={removeLastAdded}>
155+
<TouchableOpacity
156+
style={[styles.button, controlsDisabled && styles.buttonDisabled]}
157+
onPress={removeLastAdded}
158+
disabled={controlsDisabled}
159+
>
169160
<Text style={styles.buttonText}>Delete Last Added</Text>
170161
</TouchableOpacity>
171162
</View>
@@ -179,10 +170,11 @@ function MenuList({
179170
onChangeText={(text) => (indexToDeleteValue.current = text)}
180171
/>
181172
<TouchableOpacity
182-
style={styles.button}
173+
style={[styles.button, controlsDisabled && styles.buttonDisabled]}
183174
onPress={() =>
184175
removeByIndex(parseInt(indexToDeleteValue.current, 10))
185176
}
177+
disabled={controlsDisabled}
186178
>
187179
<Text style={styles.buttonText}>Remove by Index</Text>
188180
</TouchableOpacity>
@@ -204,13 +196,14 @@ function MenuList({
204196
onChangeText={(text) => (index2Value.current = text)}
205197
/>
206198
<TouchableOpacity
207-
style={styles.button}
199+
style={[styles.button, controlsDisabled && styles.buttonDisabled]}
208200
onPress={() =>
209201
swapIndexes(
210202
parseInt(index1Value.current, 10),
211203
parseInt(index2Value.current, 10)
212204
)
213205
}
206+
disabled={controlsDisabled}
214207
>
215208
<Text style={styles.buttonText}>Swap Indexes</Text>
216209
</TouchableOpacity>
@@ -232,13 +225,14 @@ function MenuList({
232225
onChangeText={(text) => (updateLabelValue.current = text)}
233226
/>
234227
<TouchableOpacity
235-
style={styles.button}
228+
style={[styles.button, controlsDisabled && styles.buttonDisabled]}
236229
onPress={() =>
237230
updateLabelAtIndex(
238231
parseInt(updateIndexValue.current, 10),
239232
updateLabelValue.current
240233
)
241234
}
235+
disabled={controlsDisabled}
242236
>
243237
<Text style={styles.buttonText}>Update Label</Text>
244238
</TouchableOpacity>
@@ -308,6 +302,9 @@ const styles = StyleSheet.create({
308302
paddingHorizontal: 14,
309303
borderRadius: 6,
310304
},
305+
buttonDisabled: {
306+
backgroundColor: '#555',
307+
},
311308
buttonText: {
312309
color: '#fff',
313310
fontSize: 13,

0 commit comments

Comments
 (0)