-
Notifications
You must be signed in to change notification settings - Fork 44
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
[Feature] Support loading Rive animations from Expo Assets #241
Comments
We are considering using Rive in our Expo app. Our current two options are a) Download the rive assets with urls, which isn't a great user experience especially on startup. Neither option seems that great. This feature seems critical if you are going to get any wider adoption in react-native. |
Glad I found this issue. Thank you. Will be sticking with Lottie |
I finally have a solution. Hope this makes its way into Rive eventually...but it requires 2 steps:
This works just like images in expo and react-native. In development, the file is served by metro, so it's super fast and dynamic to change images. In production, the asset is bundled and the appropriate local uri is handled for you. It just works. I just tested in both environments and it works great. SOOOO much better than every other solution I've seen here on github. *note that this is edited. The original code was missing the resourceName prop for bundled Android apps and was only working in dev mode |
Hey @tslater Thanks a lot for the code snippet! |
Thanks @tslater for the code 👍 It's seems code crash when I used an external URL in the This version works for me on iOS (simu) and Android (emu): import React, { forwardRef, useMemo } from "react";
import Rive, { RiveRef } from "rive-react-native";
// @ts-ignore
import resolveAssetSource from "react-native/Libraries/Image/resolveAssetSource";
type RiveComponentProps = Omit<
React.ComponentProps<typeof Rive>,
"url" | "resourceName"
> & {
source: string | number;
};
const isValidUrl = (uri: string | undefined): boolean => {
if (!uri) return false;
return uri.startsWith("http") || uri.startsWith("file");
};
export const RiveAnimation = forwardRef<RiveRef, RiveComponentProps>(
(props, ref) => {
const { source, ...riveProps } = props;
const riveConfig = useMemo(() => {
if (typeof source === "string" && isValidUrl(source)) {
return { url: source };
}
const resolved = resolveAssetSource(source);
const uri = resolved?.uri;
const isUrl = isValidUrl(uri);
return {
resourceName: !isUrl && uri ? uri : undefined,
url: isUrl ? uri : undefined,
};
}, [source]);
return <Rive ref={ref} {...riveProps} {...riveConfig} />;
}
); Then I can use these three types of source: // Import
import truckV7 from "../assets/animations/truck_v7.riv";
// ...
<RiveAnimation source={truckV7} />; // Require
<RiveAnimation source={require("../assets/animations/truck_v7.riv")} /> // Remote URL
<RiveAnimation source={"https://public.uat.rive.app/community/runtime-files/148-325-tape.riv"} /> Also, when blink_on_iOS.mp4 |
@guval-gh If you look at the react native docs, when you use a remote url, you should use the url prop.
Can you try that and let me know if it works or not? |
@tslater |
@guval-gh I'm unsure of what you mean. Have you tried using the url prop with the wrapped component code I provided? I forwards all the props, so it should work. What error or behavior do you see if you use the url prop? Did you get it working with modifications? Do you want to share those? |
@tslater I mean, of course I can use the With your code, if the source pass is a remote url, the code crash. Yes, it works, I share it just above #241 (comment) |
This is a follow-up to #123 and #185 - please refer to these issues for additional context.
Description
This issue seeks to add support for Expo Assets to the React Native Rive integration.
Expo Assets enables developers to load custom assets asynchronously without needing to explicitly bundle them into the native app code. This enables features like hot-reloading during local development, as well as the ability to deploy updates through EAS update
Without this, there is a lot of friction that discourages the use of Rive on an Expo project:
During local development, you can't hot-reload Rive animations. You need to do a full build of your custom dev client.
After deployment, it is not possible to update the Rive animations embedded in the app without a new native build being published to the app store. There is no way to update the animations over EAS Update.
Current behavior
On iOS, it is already possible to use Expo Assets using the instructions found in the initial issue (#123)
On Android, this does not work. The Rive adaptor chokes when it receives a
file://
URL.Expected behavior
I can pass in a
file://
URL from Expo Asset into the Rive adapter for React Native, and it works on all platforms.The text was updated successfully, but these errors were encountered: