-
-
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
useAnimatedSensor | TypeError: Cannot assign to read-only property 'sensor' fixed. #5537
Conversation
TypeError: Cannot assign to read-only property 'sensor'
Unexpected empty arrow function fix.
Hi @talut, thanks for submitting the PR! I cannot recreate the issue you are trying to fix however. When I'm trying to use the code snippet you posted on the main branch I'm not getting any errors. Am I missing something? |
Oh I see, I found #5538 and it happens on re-render. I updated your snippet accordingly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What concerns me here is the fact that with these changes we are no longer returning a stable reference. This is potentially a breaking change.
Maybe we should instead re-work the mechanism due to which this object is getting frozen in the first place, I'll make a PoC of this.
cc @piaskowyk
I opened an alternate pull request to this one. We have to think which one do we want to apply here. Keep in mind that the problem arises only because return value of const { sensor } = useAnimatedSensor(SensorType.GYROSCOPE, {
interval: 10,
});
useDerivedValue(() => {
const x = sensor.value.x;
const y = sensor.value.y;
console.log('x', x);
console.log('y', y);
}); // No need to specify dependencies when Reanimated Babel plugin isn't disabled. Then everything would be fine. |
Hey @tjzel thanks for the updates. You're right my solution returns an unstable reference.
I'll try this one. Thanks. |
TypeError: Cannot assign to read-only property 'sensor'
Summary
This pull request aims to resolve the issue related to the TypeError: Cannot assign to read-only property 'sensor'. When attempting to modify a read-only property. In this case, the issue arises from reassigning ref.current.sensor within the useRef hook.
While the object returned by useRef is mutable, modifying a read-only property within this object, as done with ref.current.sensor, leads to the error in question.
In the current implementation, ref.current.sensor is initially set with the function initializeSensor(sensorType, config) and is later reassigned in the useEffect hook. It is this reassignment that triggers the error.
To fix this issue, I suggest a code restructuring to avoid the direct reassignment of the sensor property. Instead, we can create a new object for ref.current each time an update is needed. This approach sidesteps the limitations of the read-only property while retaining the intended functionality.
Test plan