rive-react-native

repository·main·Indexed 21 days ago

https://github.com/rive-app/rive-react-native

A React Native wrapper for the Rive animation runtime (version 9.8.5) that provides a component and ref-based pattern to integrate interactive animations into mobile applications. It supports loading animations via local resource names or remote URLs, manipulating state machines, and controlling playback through the RiveRef API. Compatible with iOS 14.0+ and Android SDK 21+.

Tokens
7.8K
Snippets
30
Records
35
Agent score
71%

What's inside rive-react-native

  1. Use the Rive component

    main

    The Rive component provides a bridge to the native Rive runtime on iOS and Android. You can load animations using either a local resourceName (a filename without the .riv extension) or a remote url. Note that you cannot provide both simultaneously.

    Basic usage:

    import Rive from 'rive-react-native';
    
    function App() {
      return <Rive resourceName="truck_v7" />;
    }
  2. Trigger play and pause manually using RiveRef

    main

    You can control the animation imperatively by using a ref with the RiveRef type. This allows you to call methods like play() and pause() on the component instance.

    import Rive, { RiveRef } from 'rive-react-native'
    import React from 'react';
    import { View, Button } from 'react-native';
    
    export default function App() {
      const riveRef = React.useRef<RiveRef>(null);
    
      const handlePlayPress = () => {
        riveRef?.current?.play();
      };
    
      return (
        <View>
          <Rive
            resourceName="truck_v7"
            ref={riveRef}
          />
    
          <Button onPress={handlePlayPress} title="play" />
        </View>
      );
    }
  3. Run the Rive React Native example app

    main

    The example/ folder contains a sample application demonstrating how to use the Rive component and useRef hook pattern. It covers setting Rive files via URL or local assets, displaying multiple artboards, and manipulating state machines via inputs.

    To run the example:

    1. Run yarn bootstrap in the root directory.
    2. Navigate to the example folder: cd example.
    3. Run the app using Expo: yarn expo run:android or yarn expo run:ios.

    Note for iOS: You may need to run pod install (for the first time) or pod update RiveRuntime in the example/iOS folder to install or update the underlying Rive iOS runtime.

    yarn bootstrap
    cd example
    yarn expo run:android
    # or
    yarn expo run:ios
  4. Configure Rive asset sources

    main

    Rive assets can be loaded using several source types defined by RiveAssetPropType. You can provide a source via FileHandlerOptions using one of the following:

    • Require Source: A numeric ID from a require() call (e.g., require('./asset.riv')).
    • URI Source: An object with a uri string.
    • Packaged Source: An object containing a fileName and an optional path (required for Android assets).
    // Using a required asset
    <Rive asset={require('./my_animation.riv')} />
    
    // Using a URI
    <Rive asset={{ uri: 'https://example.com/animation.riv' }} />
    
    // Using a packaged asset (Android)
    <Rive asset={{ fileName: 'my_animation.riv', path: 'assets/animations' }} />
  5. Customize Rive Native SDK versions for Android

    main

    To override the Android Rive runtime version in a Vanilla React Native project, add the Rive_RiveRuntimeAndroidVersion property to your android/gradle.properties file.

    Version Resolution Priority (Android):

    1. android/gradle.properties (Rive_RiveRuntimeAndroidVersion)
    2. package.json (runtimeVersions.android)

    Warning: Custom versions may become incompatible when you update rive-react-native. Always verify defaults in new releases.

    Rive_RiveRuntimeAndroidVersion=11.7.2
  6. Customize Rive Native SDK versions for iOS

    main

    By default, the library uses versions specified in package.json. If you need to override the iOS Rive runtime version in a Vanilla React Native project, create or edit ios/Podfile.properties.json and set the RiveRuntimeIOSVersion key.

    Version Resolution Priority (iOS):

    1. ios/Podfile.properties.json (RiveRuntimeIOSVersion)
    2. package.json (runtimeVersions.ios)

    Warning: Custom versions may become incompatible when you update rive-react-native. Always verify defaults in new releases.

    {
      "RiveRuntimeIOSVersion": "6.21.1"
    }
  7. Customize Rive Native SDK versions in Expo

    main

    For Expo projects, use config plugins within your app.config.ts to override the native SDK versions. This ensures the correct versions are applied during the prebuild process.

    Use withPodfileProperties for iOS and withGradleProperties for Android.

    import { ExpoConfig, ConfigContext } from 'expo/config';
    import { withPodfileProperties } from '@expo/config-plugins';
    import { withGradleProperties } from '@expo/config-plugins';
    
    export default ({ config }: ConfigContext): ExpoConfig => ({
      ...config,
      plugins: [
        [
          withPodfileProperties,
          {
            RiveRuntimeIOSVersion: '6.21.1',
          },
        ],
        [
          withGradleProperties,
          {
            Rive_RiveRuntimeAndroidVersion: '11.7.2',
          },
        ],
      ],
    });
  8. Use the Rive component to render animations

    main

    The Rive component (exported as default) is the primary way to display Rive animations in React Native. You can provide a Rive file via url, resourceName, or a source object (which can be a local asset number or a URI object).

    Key props include:

    • autoplay: Boolean, defaults to true.
    • fit: Controls how the animation fits the container (e.g., Fit.Contain).
    • alignment: Controls alignment within the container.
    • artboardName: The name of the specific artboard to render.
    • stateMachineName: The name of the state machine to run.
    • animationName: The name of a specific animation to play.
    • onStateChanged: Callback triggered when the state machine state changes.
    • onRiveEventReceived: Callback for custom Rive events.
    • onError: Callback for handling Rive-specific errors.
    import Rive from 'rive-react-native';
    import { Fit, Alignment } from 'rive-react-native/types'; // Types imported from package
    
    <Rive
      url="https://example.com/animation.riv"
      stateMachineName="State Machine 1"
      autoplay={true}
      fit={Fit.Contain}
      alignment={Alignment.Center}
      onStateChanged={(stateMachineName, stateName) => {
        console.log(`State changed to ${stateName} in ${stateMachineName}`);
      }}
    />