Skip to content

Commit

Permalink
fix: Invalid shared value access displaying when code is correct (#6631)
Browse files Browse the repository at this point in the history
## Summary

The problem was caused by our `useDerivedValue` hook which set ref to
`null` in the `useEffect` cleanup method.

```tsx
useEffect(() => {
  return () => {
    initRef.current = null;
  };
}, []);
```

The warning was shown during hot reload, because react calls cleanup
functions in hooks, thus the `initRef` was reset in the
`useDerivedValue` hook, which resulted in execution of this code that
should run only when the component mounts for the first time:

```tsx
if (initRef.current === null) {
  initRef.current = makeMutable(initialUpdaterRun(updater));
}
```

Because of that, `initialUpdaterRun` executed the `updater` callback on
the JS thread during hot reload, while the component was rendered, which
resulted in reading shared value's `.value` property during the render
phase.
  • Loading branch information
MatiPl01 authored and tjzel committed Nov 26, 2024
1 parent c1411f9 commit ac0096e
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 8 deletions.
6 changes: 0 additions & 6 deletions packages/react-native-reanimated/src/hook/useDerivedValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,5 @@ export function useDerivedValue<Value>(
};
}, dependencies);

useEffect(() => {
return () => {
initRef.current = null;
};
}, []);

return sharedValue;
}
4 changes: 2 additions & 2 deletions packages/react-native-reanimated/src/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ export function updateLoggerConfig(options?: Partial<LoggerConfig>) {
registerLoggerConfig({
...__reanimatedLoggerConfig,
// Don't reuse previous level and strict values from the global config
level: options?.level ?? LogLevel.warn,
strict: options?.strict ?? false,
level: options?.level ?? DEFAULT_LOGGER_CONFIG.level,
strict: options?.strict ?? DEFAULT_LOGGER_CONFIG.strict,
});
}

Expand Down

0 comments on commit ac0096e

Please sign in to comment.