-
Notifications
You must be signed in to change notification settings - Fork 66
Add the Quick Start example to the React Native example app #374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /* | ||
| Rive React Native Quick Start | ||
|
|
||
| Resources: | ||
|
|
||
| - Getting Started with Rive + React Native: https://rive.app/docs/runtimes/react-native/react-native | ||
| - Data Binding: https://rive.app/docs/runtimes/data-binding | ||
| - Setting and reading view model properties: https://rive.app/docs/runtimes/data-binding#observability | ||
| */ | ||
|
|
||
| import { useEffect } from 'react'; | ||
| import { Button, SafeAreaView, ScrollView, StyleSheet } from 'react-native'; | ||
| import Rive, { | ||
| useRive, | ||
| Fit, | ||
| AutoBind, | ||
| useRiveNumber, | ||
| useRiveTrigger, | ||
| RNRiveError, | ||
| RNRiveErrorType, | ||
| } from 'rive-react-native'; | ||
|
|
||
| export default function QuickStart() { | ||
| const [setRiveRef, riveRef] = useRive(); | ||
|
|
||
| // This is how your read and set properties from your Rive View Model | ||
| const [health, setHealth] = useRiveNumber(riveRef, 'health'); | ||
|
|
||
| // Reference a Trigger from your View Model | ||
| const gameOverTrigger = useRiveTrigger(riveRef, 'gameOver', () => { | ||
| // Listen for a Trigger event, whether it comes from the Rive app or from the code | ||
| console.log('Game Over Triggered'); | ||
| }); | ||
|
|
||
| useEffect(() => { | ||
| if (riveRef && setHealth) { | ||
| // Set the initial health value | ||
| setHealth(9); | ||
| } | ||
| }, [riveRef, setHealth]); | ||
|
|
||
| const handleTakeDamage = () => { | ||
| if (health && setHealth) { | ||
| setHealth(health - 7); | ||
| // If all state machines have settled, you might need to wake the state machine back up. | ||
| // This can happen when all animations have finished playing. | ||
| riveRef?.play(); | ||
| } | ||
| }; | ||
|
|
||
| const handleMaxHealth = () => { | ||
| if (setHealth) { | ||
| setHealth(100); | ||
| riveRef?.play(); | ||
| } | ||
| }; | ||
|
Comment on lines
+51
to
+56
|
||
|
|
||
| const handleGameOver = () => { | ||
| if (setHealth && gameOverTrigger) { | ||
| setHealth(0); | ||
| gameOverTrigger(); | ||
| riveRef?.play(); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <SafeAreaView style={styles.safeAreaViewContainer}> | ||
| <ScrollView contentContainerStyle={styles.container}> | ||
| <Rive | ||
| ref={setRiveRef} | ||
| // This is expecting a resource named quick_start.riv | ||
| // For iOS, make sure it's listed in XCode under "Build Phases > Copy Bundle Resources" | ||
| // For Android, make sure it's in the /android/app/src/main/res/raw/ directory | ||
| resourceName={'quick_start'} | ||
| // You can also load an external .riv file | ||
| // url={'https://public.rive.app/community/runtime-files/2195-4346-avatar-pack-use-case.riv'} | ||
| fit={Fit.Layout} | ||
| style={styles.animation} | ||
| autoplay={true} | ||
| // AutoBind uses the view model instance from your Rive file | ||
| dataBinding={AutoBind(true)} | ||
| // dataBinding={BindByIndex(0)} | ||
| // dataBinding={BindByName('SomeName')} | ||
| // dataBinding={BindEmpty()} | ||
| onError={(riveError: RNRiveError) => { | ||
| switch (riveError.type) { | ||
| case RNRiveErrorType.DataBindingError: { | ||
| console.error(`${riveError.message}`); | ||
| return; | ||
| } | ||
| default: | ||
| console.error('Unhandled error'); | ||
| return; | ||
| } | ||
| }} | ||
| /> | ||
| </ScrollView> | ||
| <Button onPress={handleTakeDamage} title="Take Damage" /> | ||
| <Button onPress={handleMaxHealth} title="Max Health" /> | ||
| <Button onPress={handleGameOver} title="Game Over" /> | ||
| </SafeAreaView> | ||
| ); | ||
| } | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| safeAreaViewContainer: { | ||
| flex: 1, | ||
| }, | ||
| container: { | ||
| flexGrow: 1, | ||
| alignItems: 'center', | ||
| justifyContent: 'center', | ||
| }, | ||
| animation: { | ||
| width: '100%', | ||
| }, | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The magic number 7 for damage amount should be extracted to a named constant to improve code readability and maintainability.