Skip to content

fix: guard against null ScreenCornerRadius native module on simulator - #16

Open
blazejkustra wants to merge 1 commit into
mrousavy:masterfrom
blazejkustra:fix/guard-null-native-module
Open

fix: guard against null ScreenCornerRadius native module on simulator#16
blazejkustra wants to merge 1 commit into
mrousavy:masterfrom
blazejkustra:fix/guard-null-native-module

Conversation

@blazejkustra

Copy link
Copy Markdown

Summary

On the iOS simulator (and any setup where the native module isn't registered), NativeModules.ScreenCornerRadius is null. The current code reads .cornerRadius off it directly:

export const ScreenCornerRadius: number =
  NativeModules.ScreenCornerRadius.cornerRadius ?? 0;

The ?? 0 only guards .cornerRadius being null/undefined — it does not guard NativeModules.ScreenCornerRadius itself being null. So on the simulator this throws:

TypeError: Cannot read property 'cornerRadius' of null

at import time, which surfaces as an error in Metro. The doc comment promises "May also be 0 if the corner radius couldn't be detected", but it throws instead of falling back to 0.

Fix

Optional-chain the module access so it falls back to 0 (and IsScreenRounded to false) as documented:

export const ScreenCornerRadius: number =
  NativeModules.ScreenCornerRadius?.cornerRadius ?? 0;

One-line change in src/index.ts.

Test plan

  • Run an app importing ScreenCornerRadius on an iOS simulator → no longer throws; ScreenCornerRadius is 0 and IsScreenRounded is false.
  • On a physical device the native module is present, so behavior is unchanged.

On the iOS simulator (and any setup where the native module isn't
registered) NativeModules.ScreenCornerRadius is null. Reading
.cornerRadius on it throws "Cannot read property 'cornerRadius' of
null" at import time, before the ?? 0 fallback can apply.

Optional-chain the module access so it falls back to 0 (and
IsScreenRounded to false) as the docs promise.
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.

1 participant