@@ -5,14 +5,15 @@ import {
55 ActivityIndicator ,
66 TouchableOpacity ,
77} from 'react-native' ;
8- import { useMemo , useState , useCallback , useRef } from 'react' ;
8+ import { useMemo , useState , useRef } from 'react' ;
99import {
1010 Fit ,
1111 RiveView ,
1212 type ViewModelInstance ,
1313 type RiveFile ,
1414 type RiveViewRef ,
1515 useRiveFile ,
16+ useRiveList ,
1617} from '@rive-app/react-native' ;
1718import { type Metadata } from '../helpers/metadata' ;
1819
@@ -65,18 +66,10 @@ function ListExample({
6566} ) {
6667 const riveRef = useRef < RiveViewRef > ( null ) ;
6768 const [ isPlaying , setIsPlaying ] = useState ( true ) ;
68- const listProperty = useMemo (
69- ( ) => instance . listProperty ( 'ListItemVM' ) ,
70- [ instance ]
71- ) ;
72- const [ listLength , setListLength ] = useState ( listProperty ?. length ?? 0 ) ;
73-
74- const refreshLength = useCallback ( ( ) => {
75- setListLength ( listProperty ?. length ?? 0 ) ;
76- } , [ listProperty ] ) ;
69+ const { length, addInstance, removeInstanceAt, swap, getInstanceAt, error } =
70+ useRiveList ( 'ListItemVM' , instance ) ;
7771
78- const handleAddItem = useCallback ( ( ) => {
79- if ( ! listProperty ) return ;
72+ const handleAddItem = ( ) => {
8073 const buttonVM = file . viewModelByName ( 'button VM' ) ;
8174 if ( ! buttonVM ) {
8275 console . error ( 'button VM view model not found' ) ;
@@ -91,54 +84,47 @@ function ListExample({
9184 if ( stringProp ) {
9285 stringProp . value = 'new btn' ;
9386 }
94- listProperty . addInstance ( newInstance ) ;
87+ addInstance ( newInstance ) ;
9588 riveRef . current ?. playIfNeeded ( ) ;
96- refreshLength ( ) ;
97- } , [ listProperty , file , refreshLength ] ) ;
89+ } ;
9890
99- const handleRemoveFirst = useCallback ( ( ) => {
100- if ( ! listProperty || listProperty . length === 0 ) return ;
101- listProperty . removeInstanceAt ( 0 ) ;
91+ const handleRemoveFirst = ( ) => {
92+ if ( length === 0 ) return ;
93+ removeInstanceAt ( 0 ) ;
10294 riveRef . current ?. playIfNeeded ( ) ;
103- refreshLength ( ) ;
104- } , [ listProperty , refreshLength ] ) ;
95+ } ;
10596
106- const handleRemoveLast = useCallback ( ( ) => {
107- if ( ! listProperty || listProperty . length === 0 ) return ;
108- listProperty . removeInstanceAt ( listProperty . length - 1 ) ;
97+ const handleRemoveLast = ( ) => {
98+ if ( length === 0 ) return ;
99+ removeInstanceAt ( length - 1 ) ;
109100 riveRef . current ?. playIfNeeded ( ) ;
110- refreshLength ( ) ;
111- } , [ listProperty , refreshLength ] ) ;
101+ } ;
112102
113- const handleSwapFirstTwo = useCallback ( ( ) => {
114- if ( ! listProperty || listProperty . length < 2 ) return ;
115- listProperty . swap ( 0 , 1 ) ;
103+ const handleSwapFirstTwo = ( ) => {
104+ if ( length < 2 ) return ;
105+ swap ( 0 , 1 ) ;
116106 riveRef . current ?. playIfNeeded ( ) ;
117- refreshLength ( ) ;
118- } , [ listProperty , refreshLength ] ) ;
119-
120- const logListItems = useCallback ( ( ) => {
121- if ( ! listProperty ) return ;
122- console . log ( `List has ${ listProperty . length } items:` ) ;
123- for ( let i = 0 ; i < listProperty . length ; i ++ ) {
124- const item = listProperty . getInstanceAt ( i ) ;
107+ } ;
108+
109+ const logListItems = ( ) => {
110+ console . log ( `List has ${ length } items:` ) ;
111+ for ( let i = 0 ; i < length ; i ++ ) {
112+ const item = getInstanceAt ( i ) ;
125113 console . log ( ` [${ i } ]: ${ item ?. instanceName ?? 'undefined' } ` ) ;
126114 }
127- } , [ listProperty ] ) ;
115+ } ;
128116
129- const handlePlayPause = useCallback ( ( ) => {
117+ const handlePlayPause = ( ) => {
130118 if ( isPlaying ) {
131119 riveRef . current ?. pause ( ) ;
132120 } else {
133121 riveRef . current ?. play ( ) ;
134122 }
135123 setIsPlaying ( ! isPlaying ) ;
136- } , [ isPlaying ] ) ;
124+ } ;
137125
138- if ( ! listProperty ) {
139- return (
140- < Text style = { styles . errorText } > ListItemVM list property not found</ Text >
141- ) ;
126+ if ( error ) {
127+ return < Text style = { styles . errorText } > { error . message } </ Text > ;
142128 }
143129
144130 return (
@@ -156,7 +142,7 @@ function ListExample({
156142 file = { file }
157143 />
158144 < View style = { styles . controls } >
159- < Text style = { styles . infoText } > List length: { listLength } </ Text >
145+ < Text style = { styles . infoText } > List length: { length } </ Text >
160146 < View style = { styles . buttonRow } >
161147 < TouchableOpacity style = { styles . button } onPress = { handleAddItem } >
162148 < Text style = { styles . buttonText } > Add Item</ Text >
0 commit comments