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.

Cannot add new property '__reanimatedHostObjectRef'

See original GitHub issue

Description

I can’t use an array of objects coming in through a prop inside worklets. A prop with a simple value like a number works fine. Also making a deep copy with JSON.parse(JSON.stringify(props.items)) solves the problem.

Looks like Reanimated tries to add a property on an immutable prop object and fails.

Steps to reproduce

type Props = { 
  items: Item[]
}
const MyComponent = ({ items }: Props) => {

  const foo = () => {
    'worklet'
    const item = items[0] // <-- this line causes the error
  }

  return ...
}

Expected behavior

I should be able to use any type of props in worklets

Actual behavior

Cannot add new property '__reanimatedHostObjectRef'

Package versions

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:18 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
thisisgitcommented, Mar 25, 2022

I had the same issue when trying to assign an object or array of objects from Redux state to a shared value.

e.g. using Redux Toolkit(RTK):

// useSelector hook from RTK
const arrayOfObjects: Test[] = useSelector(selectorReturningTests);
const assignThemToMe = useSharedValue<Test[]>([]);

// Throws above error
assignThemToMe.value = arrayOfObjects;

I tried using mock data and it worked just fine. It only throws error when trying to assign value returned from RTK’s selector. While trying to figure out where the problem is coming from, I found this: a

When an object is assigned to a shared value, that object gets mutated by reanimated and a property called __reanimatedHostObjRef gets added. Same for array of objects, every object in the array gets that property after it is assigned to shared value. And looks like it’s being added by this function: https://github.com/software-mansion/react-native-reanimated/blob/45c57a906b1c3881c37f08b09749a9c68faf7ce1/Common/cpp/SharedItems/ShareableValue.cpp#L21-L34 I’m assuming that it’s throwing the error because it failed to mutate the object, i.e. failed to add __reanimatedHostObjRef property.

But why this issue only occurs when trying to assign data returned from RTK? I think it’s because RTK uses immer internally, which is used to freeze the data. And this is related to https://github.com/software-mansion/react-native-reanimated/issues/1517#issuecomment-1041810730, where it throws the same error when trying to play around with frozen object.

Probably that’s why the solution of deep copying the object/array of objects using JSON.parse(JSON.stringify(items)) works. I think it would be better to show more readable error message if this issue doesn’t get resolved anytime soon.

3reactions
dohooocommented, Mar 25, 2022

I had the same issue when trying to assign an object or array of objects from Redux state to a shared value.

e.g. using Redux Toolkit(RTK):

// useSelector hook from RTK
const arrayOfObjects: Test[] = useSelector(selectorReturningTests);
const assignThemToMe = useSharedValue<Test[]>([]);

// Throws above error
assignThemToMe.value = arrayOfObjects;

I tried using mock data and it worked just fine. It only throws error when trying to assign value returned from RTK’s selector. While trying to figure out where the problem is coming from, I found this: a

When an object is assigned to a shared value, that object gets mutated by reanimated and a property called __reanimatedHostObjRef gets added. Same for array of objects, every object in the array gets that property after it is assigned to shared value. And looks like it’s being added by this function:

https://github.com/software-mansion/react-native-reanimated/blob/45c57a906b1c3881c37f08b09749a9c68faf7ce1/Common/cpp/SharedItems/ShareableValue.cpp#L21-L34

I’m assuming that it’s throwing the error because it failed to mutate the object, i.e. failed to add __reanimatedHostObjRef property. But why this issue only occurs when trying to assign data returned from RTK? I think it’s because RTK uses immer internally, which is used to freeze the data. And this is related to #1517 (comment), where it throws the same error when trying to play around with frozen object.

Probably that’s why the solution of deep copying the object/array of objects using JSON.parse(JSON.stringify(items)) works. I think it would be better to show more readable error message if this issue doesn’t get resolved anytime soon.

Dude, you’re so cool

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot add new property '__reanimatedHostObjectRef' React ...
I am developing a project where I have a listiview and showing 150-200 customers. There is a feature where I need to drag...
Read more >
runOnJS | React Native Reanimated - Software Mansion
runOnJS returns a function which can be safely run from the UI thread. Example​. Here is an example of calling a javascript callback...
Read more >
Cannot add property <propName>, object is not extensible
So I was referencing an object that didn't exist anymore and there was some kind of grab the reference before the old one...
Read more >
cannot add property 1 object is not extensible - You.com
defineProperty () throws when adding a new property to a non-extensible object. To fix this error, you will either need to remove the...
Read more >
Realm: Create reactive mobile apps in a fraction of the time
Change directories into the new project ( cd <project-name> ) and add the realm ... a property from an owning object and they...
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