1- import {
2- View ,
3- Text ,
4- StyleSheet ,
5- ActivityIndicator ,
6- Button ,
7- } from 'react-native' ;
8- import { useEffect , useMemo , useState , useRef } from 'react' ;
1+ import { View , Text , StyleSheet , ActivityIndicator } from 'react-native' ;
2+ import { useEffect , useMemo } from 'react' ;
93import {
104 Fit ,
115 RiveView ,
@@ -98,67 +92,15 @@ function DataBindingExample({
9892 setBarColor ( '#0000FF' ) ;
9993 } , [ setBarColor , setButtonText ] ) ;
10094
101- // Direct addListener usage (without hooks)
102- const [ coinValue , setCoinValue ] = useState < number | null > ( null ) ;
103- const [ isListening , setIsListening ] = useState ( true ) ;
104- const removeListenerRef = useRef < ( ( ) => void ) | null > ( null ) ;
105-
106- useEffect ( ( ) => {
107- const coinProperty = instance . numberProperty ( 'Coin/Item_Value' ) ;
108- if ( ! coinProperty ) return ;
109-
110- // Add listener and store the remover function
111- removeListenerRef . current = coinProperty . addListener ( ( value ) => {
112- console . log ( 'Coin value changed:' , value ) ;
113- setCoinValue ( value ) ;
114- } ) ;
115-
116- return ( ) => {
117- // Clean up on unmount
118- removeListenerRef . current ?.( ) ;
119- } ;
120- } , [ instance ] ) ;
121-
122- const toggleListener = ( ) => {
123- if ( isListening && removeListenerRef . current ) {
124- // Remove the listener by calling the remover function
125- removeListenerRef . current ( ) ;
126- removeListenerRef . current = null ;
127- setIsListening ( false ) ;
128- } else if ( ! isListening ) {
129- // Re-add the listener
130- const coinProperty = instance . numberProperty ( 'Coin/Item_Value' ) ;
131- if ( coinProperty ) {
132- removeListenerRef . current = coinProperty . addListener ( ( value ) => {
133- console . log ( 'Coin value changed:' , value ) ;
134- setCoinValue ( value ) ;
135- } ) ;
136- setIsListening ( true ) ;
137- }
138- }
139- } ;
140-
14195 return (
142- < View style = { styles . flex } >
143- < View style = { styles . listenerDemo } >
144- < Text style = { styles . listenerText } >
145- Coin Value: { coinValue ?? 'N/A' } { ' ' }
146- { isListening ? '(listening)' : '(paused)' }
147- </ Text >
148- < Button
149- title = { isListening ? 'Remove Listener' : 'Add Listener' }
150- onPress = { toggleListener }
151- />
152- </ View >
153- < RiveView
154- style = { styles . rive }
155- autoPlay = { true }
156- dataBind = { instance }
157- fit = { Fit . Layout }
158- layoutScaleFactor = { 1 }
159- file = { file }
160- />
161- </ View >
96+ < RiveView
97+ style = { styles . rive }
98+ autoPlay = { true }
99+ dataBind = { instance }
100+ fit = { Fit . Layout }
101+ layoutScaleFactor = { 1 }
102+ file = { file }
103+ />
162104 ) ;
163105}
164106
@@ -177,9 +119,6 @@ const styles = StyleSheet.create({
177119 flex : 1 ,
178120 backgroundColor : '#f5f5f5' ,
179121 } ,
180- flex : {
181- flex : 1 ,
182- } ,
183122 rive : {
184123 flex : 1 ,
185124 width : '100%' ,
@@ -190,15 +129,4 @@ const styles = StyleSheet.create({
190129 textAlign : 'center' ,
191130 padding : 20 ,
192131 } ,
193- listenerDemo : {
194- padding : 16 ,
195- backgroundColor : '#e8e8e8' ,
196- flexDirection : 'row' ,
197- alignItems : 'center' ,
198- justifyContent : 'space-between' ,
199- } ,
200- listenerText : {
201- fontSize : 16 ,
202- fontWeight : '500' ,
203- } ,
204132} ) ;
0 commit comments