Skip to content
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

fix(android,animated-sensor): Fix HMR for Animated Sensor #5646

Merged
merged 4 commits into from
Feb 12, 2024

Conversation

Nodonisko
Copy link
Contributor

@Nodonisko Nodonisko commented Feb 6, 2024

Summary

While playing with useAnimatedSensor(SensorType.GYROSCOPE) I was really annoyed that every time my app hot reloaded I got error like this: Cannot assign to read-only property 'sensor' or Cannot assign to read-only property 'unregister'.

There seems to be some issue that properties inside "ref.current" becomes read-only so I added code that will replace whole ref.current instead of replacing properties one by one at begging of useEffect. This seems to solve the issue.

Also it seems that this is Android only issue. I did not encounter that on iOS.

Here are screenshots of errors https://photos.google.com/share/AF1QipM1cQFJI67AGuTyth9CY3EGM6Ipy80Jp_uSivmoHf2dTU3aRDaXSLk2TliVKO2hNg?key=bVVNWE1SRHBrcFBreW5HZFpyWGlOcGxkelVKSW9n

Test plan

There is simple code that will throw errors on Android on every hot reload without fix from this PR. You can change that console.log before useFrameCallback to trigger HMR.

import {
  SensorType,
  useAnimatedSensor,
  useFrameCallback,
} from "react-native-reanimated";

export default function App() {
  const deviceRollSensor = useAnimatedSensor(SensorType.ROTATION);
  const deviceGyroSensor = useAnimatedSensor(SensorType.GYROSCOPE);

  console.log("blah4");
  useFrameCallback((frameInfo) => {
    const deviceRoll = deviceRollSensor.sensor.value.roll;
    const deviceGyro = deviceGyroSensor.sensor.value.y;

    console.log("deviceRoll: ", deviceRoll);
    console.log("deviceGyro: ", deviceGyro);
  });

  return null;
}

@Nodonisko Nodonisko force-pushed the fix/animated-sensor-hmr branch from 18cca23 to ed2d077 Compare February 7, 2024 18:44
@tjzel
Copy link
Collaborator

tjzel commented Feb 12, 2024

Hi @Nodonisko, thanks for your contribution! There was a debate about how exactly we should fix useAnimatedSensor and eventually I wanted to do it as you did, but you were faster 😄. I took the liberty of adding better memoization handling on Reanimated side.

@tjzel tjzel added this pull request to the merge queue Feb 12, 2024
Merged via the queue into software-mansion:main with commit b7de87f Feb 12, 2024
7 checks passed
tjzel added a commit that referenced this pull request Feb 20, 2024
## Summary

While playing with `useAnimatedSensor(SensorType.GYROSCOPE)` I was
really annoyed that every time my app hot reloaded I got error like
this: `Cannot assign to read-only property 'sensor'` or `Cannot assign
to read-only property 'unregister'`.

There seems to be some issue that properties inside "ref.current"
becomes read-only so I added code that will replace whole `ref.current`
instead of replacing properties one by one at begging of `useEffect`.
This seems to solve the issue.

Also it seems that this is Android only issue. I did not encounter that
on iOS.

Here are screenshots of errors
https://photos.google.com/share/AF1QipM1cQFJI67AGuTyth9CY3EGM6Ipy80Jp_uSivmoHf2dTU3aRDaXSLk2TliVKO2hNg?key=bVVNWE1SRHBrcFBreW5HZFpyWGlOcGxkelVKSW9n

## Test plan

There is simple code that will throw errors on Android on every hot
reload without fix from this PR. You can change that `console.log`
before `useFrameCallback` to trigger HMR.

```tsx
import {
  SensorType,
  useAnimatedSensor,
  useFrameCallback,
} from "react-native-reanimated";

export default function App() {
  const deviceRollSensor = useAnimatedSensor(SensorType.ROTATION);
  const deviceGyroSensor = useAnimatedSensor(SensorType.GYROSCOPE);

  console.log("blah4");
  useFrameCallback((frameInfo) => {
    const deviceRoll = deviceRollSensor.sensor.value.roll;
    const deviceGyro = deviceGyroSensor.sensor.value.y;

    console.log("deviceRoll: ", deviceRoll);
    console.log("deviceGyro: ", deviceGyro);
  });

  return null;
}
```

---------

Co-authored-by: Tomasz Żelawski <tomasz.zelawski@swmansion.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants