.riv file should load quickly and managing the RiveFile yourself is not necessary. But if you intend to use the same .riv file in multiple parts of your application, or even on the same screen, it might be advantageous to load the file once and keep it in memory.
Example Usage
- New Runtime (Recommended)
- Legacy Runtime
In the new React Native runtime, you always need to load and manage the lifetime of a For network assets, caching the file in memory avoids repeated downloads and unnecessary decoding. The
RiveFile object that is passed to RiveView. The useRiveFile hook handles loading, and you can reuse the same RiveFile across multiple RiveView components to cache it in memory.Here’s an example showing how to cache a Rive file and reuse it across multiple components:Reuse RiveFile example
Managing State
How you keep theRiveFile alive and share it with components depends on your state management approach:- Global access: Load the file at the app level or in a context provider, and expose it using React Context or a state management library like Redux or Zustand.
- Component-level: If the file is only needed in a specific part of your app, load it in a parent component and pass it down as props.
- Custom hook: Create a custom hook that manages the
RiveFilelifecycle and provides it to consuming components.
Memory Management
TheuseRiveFile hook automatically manages the lifecycle of the RiveFile object. When the component unmounts or the input changes, the hook will dispose of the previous file and load a new one if needed. This gives you automatic memory management without manual cleanup.Network Assets
To load a Rive file from a remote URL, pass the URL string touseRiveFile:useRiveFile hook handles this automatically as long as you reuse the same riveFile object.See Loading Rive Files for more information on different loading methods.