question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[iOS] interpolateColor - Attempt to assign to readonly property

See original GitHub issue

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.

Simulator Screen Shot - iPhone 13 - 2021-12-13 at 19 08 36

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 uses interpolateColor 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:

import React, {useState, useEffect} from 'react';
import {StyleSheet} from 'react-native';
import Animated, {
  useAnimatedStyle,
  useSharedValue,
  interpolateColor,
} from 'react-native-reanimated';

const styles = StyleSheet.create({
  container: {flex: 1},
});

export default function App() {
  const [isVisible, setIsVisible] = useState(false);

  useEffect(() => {
    setTimeout(() => setIsVisible(true), 3000);
  }, []);

  return (
    <>
      <BlockOne />

      {isVisible ? <BlockTwo /> : null}
    </>
  );
}

function BlockOne() {
  const animatedValue = useSharedValue(300);
  const animatedStyles = useAnimatedStyle(() => {
    const backgroundColor = interpolateColor(
      animatedValue.value,
      [0, 234, 468],
      ['red', 'orange', 'yellow'],
    );

    return {backgroundColor};
  }, [animatedValue]);

  return <Animated.View style={[styles.container, animatedStyles]} />;
}

function BlockTwo() {
  const animatedValue = useSharedValue(0.3);
  const animatedStyles = useAnimatedStyle(() => {
    const backgroundColor = interpolateColor(
      animatedValue.value,
      [0, 1],
      ['green', 'cyan'],
    );

    return {backgroundColor};
  }, [animatedValue]);

  return <Animated.View style={[styles.container, animatedStyles]} />;
}

Package versions

  • React Native: 0.66.4 (the same with 0.63.4)
  • React Native Reanimated: ^2.3.0
  • NodeJS: 14.15.0
  • Xcode: 13.1
  • Java & Gradle: 15.0.2 & 6.7.1

Affected platforms

  • iOS

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:5
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
MingaudasVagoniscommented, Dec 21, 2021

Same as https://github.com/software-mansion/react-native-reanimated/issues/2329. It seems like enabling hermes fixes that (https://github.com/software-mansion/react-native-reanimated/issues/2329#issuecomment-981553091), if that’s not an option for you, you can apply a patch (orig - https://github.com/software-mansion/react-native-reanimated/issues/2329#issuecomment-907619489) until the issue is resolved, didn’t notice any issues in production.

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 {

0reactions
piaskowykcommented, Dec 28, 2021

Duplicate of: https://github.com/software-mansion/react-native-reanimated/issues/2329 I will try to fix it soon. Please be patient 🙏

Read more comments on GitHub >

github_iconTop Results From Across the Web

Attempted to assign to read only property on using Animated ...
Once I Stopped doing interpolate multiple times on the same value and made it independent of state, the error went away. Share.
Read more >
Attempted to assign to readonly property react native ... - abc-baltin.de
But this throws the attempted to assign to read-only property exception: const ... using interpolateColor after another component with color. import React, ...
Read more >
"Attempted to assign to readonly property." iOS 8 TypeError
A webkit bug that shipped in iOS 8 beta 5 and appears to have been partially included in the launch of iOS 8...
Read more >
Safari Technology Preview Release Notes - Apple Developer
Added support for AVIF images on macOS Ventura and iOS 16 (251850@main) ... Added support for interpolating colors with “missing”/“none” components via ...
Read more >
visibility - CSS: Cascading Style Sheets - MDN Web Docs
The visibility CSS property shows or hides an element without changing the layout of a document. The property can also hide rows or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found