From e11a2f07e8fd962adb76ff55a9b159e8ca3ae51f Mon Sep 17 00:00:00 2001 From: Tim Yung Date: Thu, 8 Jan 2026 15:28:47 -0800 Subject: [PATCH] StyleSheet: Lazily Compute `hairlineWidth` (#55100) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55100 Changes `StyleSheet` to lazily compute the `hairlineWidth` property, removing the synchronous dependency of importing `StyleSheet` on the initialization of the `PixelRatio` TurboModule. Changelog: [General][Changed] - Importing `hairlineWidth` no longer eagerly initializes the `PixelRatio` TurboModule. Reviewed By: lunaleaps Differential Revision: D90339801 fbshipit-source-id: 44cbdbec3b132f6e11d46c508dfd74f8efe8cb31 --- .../Libraries/StyleSheet/StyleSheetExports.js | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/packages/react-native/Libraries/StyleSheet/StyleSheetExports.js b/packages/react-native/Libraries/StyleSheet/StyleSheetExports.js index 89942fe1227..d9fb63ee45b 100644 --- a/packages/react-native/Libraries/StyleSheet/StyleSheetExports.js +++ b/packages/react-native/Libraries/StyleSheet/StyleSheetExports.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow + * @flow strict-local * @format */ @@ -13,16 +13,10 @@ import type {____Styles_Internal} from './StyleSheetTypes'; import composeStyles from '../../src/private/styles/composeStyles'; +import ReactNativeStyleAttributes from '../Components/View/ReactNativeStyleAttributes'; import flatten from './flattenStyle'; -const ReactNativeStyleAttributes = - require('../Components/View/ReactNativeStyleAttributes').default; -const PixelRatio = require('../Utilities/PixelRatio').default; - -let hairlineWidth: number = PixelRatio.roundToNearestPixel(0.4); -if (hairlineWidth === 0) { - hairlineWidth = 1 / PixelRatio.get(); -} +let _hairlineWidth: ?number; const absoluteFill = { position: 'absolute', @@ -95,7 +89,18 @@ export default { * * A line with hairline width may not be visible if your simulator is downscaled. */ - hairlineWidth, + // $FlowFixMe[unsafe-getters-setters] - Lazily import dependencies. + get hairlineWidth(): number { + if (_hairlineWidth == null) { + const PixelRatio = require('../Utilities/PixelRatio').default; + + _hairlineWidth = PixelRatio.roundToNearestPixel(0.4); + if (_hairlineWidth === 0) { + _hairlineWidth = 1 / PixelRatio.get(); + } + } + return _hairlineWidth; + }, /** * A very common pattern is to create overlays with position absolute and zero positioning,