-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[iOS] interpolateColor - Attempt to assign to readonly property #2739
Comments
Issue validatorThe issue is valid! |
how to fix? |
I am also experiencing this after upgrading to Reanimated 2.3 |
Same as #2329. diff --git a/node_modules/react-native-reanimated/src/reanimated2/Colors.ts b/node_modules/react-native-reanimated/src/reanimated2/Colors.ts
index 66e5c13..fc1d52d 100644
--- a/node_modules/react-native-reanimated/src/reanimated2/Colors.ts
+++ b/node_modules/react-native-reanimated/src/reanimated2/Colors.ts
@@ -694,11 +694,6 @@ const getInterpolateCacheRGBA = (
colors: readonly (string | number)[]
): InterpolateCacheRGBA => {
'worklet';
- const hash = colors.join('');
- const cache = interpolateCacheRGBA[hash];
- if (cache !== undefined) {
- return cache;
- }
const r = [];
const g = [];
@@ -715,15 +710,7 @@ const getInterpolateCacheRGBA = (
a.push(opacity(proocessedColor));
}
}
- const newCache = { r, g, b, a };
- const overrideHash = hashOrderRGBA[curentHashIndexRGBA];
- if (overrideHash) {
- delete interpolateCacheRGBA[overrideHash];
- }
- interpolateCacheRGBA[hash] = newCache;
- hashOrderRGBA[curentHashIndexRGBA] = hash;
- curentHashIndexRGBA = (curentHashIndexRGBA + 1) % BUFFER_SIZE;
- return newCache;
+ return { r, g, b, a };
};
interface InterpolateCacheHSV {
|
@MingaudasVagonis thanks! This patch works. |
Duplicate of: #2329 I will try to fix it soon. Please be patient 🙏 |
## Description The color cache was mutable only from the JS side. I decided to remove the cache and instead of this I created a new faster function with a better cache mechanism without previous limitations. Performance comparation between `interpolateColor` and `interpolateSharableColor`: ``` old 2894ms - new 1741ms // 39.8% faster old 2720ms - new 1697ms // 37.6% faster old 2739ms - new 1650ms // 39.7% faster old 2738ms - new 1781ms // 34.9% faster old 2727ms - new 1662ms // 39.0% faster ``` Performance test code: ```js const interpolateConfig = useInterpolateConfig([0, 350], ['#ff0000', '#00ff00']); const v1 = interpolateSharableColor(randomWidth.value, interpolateConfig); const iterations = 10000; const t1_start = Date.now(); for(let i = 0; i < iterations; i++) { const v = interpolateSharableColor(randomWidth.value, interpolateConfig); } const t1_end = Date.now(); const t2_start = Date.now(); for(let i = 0; i < iterations; i++) { const v = interpolateColor(randomWidth.value, [0, 350], ['#ff0000', '#00ff00']); } const t2_end = Date.now(); console.log(t1_end - t1_start, t2_end - t2_start); ``` ## Usage ```js import Animated, { useSharedValue, withTiming, useAnimatedStyle, interpolateSharableColor, useInterpolateConfig, } from 'react-native-reanimated'; import { View, Button } from 'react-native'; import React from 'react'; function AnimatedStyleUpdateExample(): React.ReactElement { const randomWidth = useSharedValue(10); const interpolateConfig = useInterpolateConfig([0, 350], ['#ff0000', '#00ff00']); // <- here const style = useAnimatedStyle(() => { return { width: randomWidth.value, backgroundColor: interpolateSharableColor(randomWidth.value, interpolateConfig) // <- here }; }); return ( <View style={{ flex: 1, flexDirection: 'column', }}> <Animated.View style={[ { width: 100, height: 80, backgroundColor: 'black', margin: 30 }, style ]} /> <Button title="toggle" onPress={() => { randomWidth.value = withTiming(Math.random() * 350) }} /> </View> ); } export default AnimatedStyleUpdateExample; ``` Fixes #2329 #2739
## Description The color cache was mutable only from the JS side. I decided to remove the cache and instead of this I created a new faster function with a better cache mechanism without previous limitations. Performance comparation between `interpolateColor` and `interpolateSharableColor`: ``` old 2894ms - new 1741ms // 39.8% faster old 2720ms - new 1697ms // 37.6% faster old 2739ms - new 1650ms // 39.7% faster old 2738ms - new 1781ms // 34.9% faster old 2727ms - new 1662ms // 39.0% faster ``` Performance test code: ```js const interpolateConfig = useInterpolateConfig([0, 350], ['#ff0000', '#00ff00']); const v1 = interpolateSharableColor(randomWidth.value, interpolateConfig); const iterations = 10000; const t1_start = Date.now(); for(let i = 0; i < iterations; i++) { const v = interpolateSharableColor(randomWidth.value, interpolateConfig); } const t1_end = Date.now(); const t2_start = Date.now(); for(let i = 0; i < iterations; i++) { const v = interpolateColor(randomWidth.value, [0, 350], ['#ff0000', '#00ff00']); } const t2_end = Date.now(); console.log(t1_end - t1_start, t2_end - t2_start); ``` ## Usage ```js import Animated, { useSharedValue, withTiming, useAnimatedStyle, interpolateSharableColor, useInterpolateConfig, } from 'react-native-reanimated'; import { View, Button } from 'react-native'; import React from 'react'; function AnimatedStyleUpdateExample(): React.ReactElement { const randomWidth = useSharedValue(10); const interpolateConfig = useInterpolateConfig([0, 350], ['#ff0000', '#00ff00']); // <- here const style = useAnimatedStyle(() => { return { width: randomWidth.value, backgroundColor: interpolateSharableColor(randomWidth.value, interpolateConfig) // <- here }; }); return ( <View style={{ flex: 1, flexDirection: 'column', }}> <Animated.View style={[ { width: 100, height: 80, backgroundColor: 'black', margin: 30 }, style ]} /> <Button title="toggle" onPress={() => { randomWidth.value = withTiming(Math.random() * 350) }} /> </View> ); } export default AnimatedStyleUpdateExample; ``` Fixes software-mansion#2329 software-mansion#2739
Description
When you try to render a component with some color interpolation using
interpolateColor
after another component with color interpolation is already rendered you get an error -Attempt to assign to readonly property
.This only happens on iOS.
Expected behavior
You shouldn't get this error on iOS.
Actual behavior & steps to reproduce
Render some component that uses
interpolateColor
and then render another component that also usesinterpolateColor
in a few seconds, for example. This also works for different screens in the navigation stack.Snack or minimal code example
Here is a reproducible demo:
https://github.com/yaroslavnikiforov/InterpolateColor/tree/main/android
Code example:
Package versions
Affected platforms
The text was updated successfully, but these errors were encountered: