useReactNativePlatformComponents
Summary
Section titled “Summary”- Rule available since:
v2.4.13 - Diagnostic Category:
lint/nursery/useReactNativePlatformComponents - This rule doesn’t have a fix.
- The default severity of this rule is error.
- This rule belongs to the following domains:
- Sources:
- Inspired from
react-native/split-platform-components
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useReactNativePlatformComponents": "error" } } }}Description
Section titled “Description”Ensure that platform-specific React Native components are only imported in files named for that platform.
Some React Native components only work on one platform. For example,
ProgressBarAndroid is Android-only and ActivityIndicatorIOS is
iOS-only. These components should live in files with a matching
platform suffix such as .android.js or .ios.js, so the React
Native bundler can ship the right code to each platform.
This rule reports an error when a platform-specific component is imported in a file that does not have the matching suffix, or when both Android and iOS components are imported in the same file.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”Importing an Android component in a non-Android file:
import { ProgressBarAndroid } from "react-native";code-block.js:1:10 lint/nursery/useReactNativePlatformComponents ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Android component ProgressBarAndroid is used outside of an Android-specific file.
> 1 │ import { ProgressBarAndroid } from “react-native”;
│ ^^^^^^^^^^^^^^^^^^
2 │
ℹ Platform-specific components produce incorrect bundles when imported in shared files.
ℹ Move this import to a file with an Android-specific suffix (e.g. .android.js).
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
Importing an iOS component in a non-iOS file:
import { ActivityIndicatorIOS } from "react-native";code-block.js:1:10 lint/nursery/useReactNativePlatformComponents ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ iOS component ActivityIndicatorIOS is used outside of an iOS-specific file.
> 1 │ import { ActivityIndicatorIOS } from “react-native”;
│ ^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Platform-specific components produce incorrect bundles when imported in shared files.
ℹ Move this import to a file with an iOS-specific suffix (e.g. .ios.js).
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
import { View } from "react-native";Options
Section titled “Options”androidPathPatterns
Section titled “androidPathPatterns”A list of glob patterns to identify Android-specific files.
Default: ["**/*.android.{js,jsx,ts,tsx}"]
In the following example, Android files use .droid.jsx as their suffix instead of the default .android.js:
{ "linter": { "rules": { "nursery": { "useReactNativePlatformComponents": { "options": { "androidPathPatterns": [ "**/*.droid.jsx" ] } } } } }}import { ProgressBarAndroid } from "react-native";import { ProgressBarAndroid } from "react-native";/Button.android.jsx:1:10 lint/nursery/useReactNativePlatformComponents ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Android component ProgressBarAndroid is used outside of an Android-specific file.
> 1 │ import { ProgressBarAndroid } from “react-native”;
│ ^^^^^^^^^^^^^^^^^^
2 │
ℹ Platform-specific components produce incorrect bundles when imported in shared files.
ℹ Move this import to a file with an Android-specific suffix (e.g. .android.js).
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
iosPathPatterns
Section titled “iosPathPatterns”A list of glob patterns to identify iOS-specific files.
Default: ["**/*.ios.{js,jsx,ts,tsx}"]
In the following example, iOS files use .apple.jsx as their suffix instead of the default .ios.js:
{ "linter": { "rules": { "nursery": { "useReactNativePlatformComponents": { "options": { "iosPathPatterns": [ "**/*.apple.jsx" ] } } } } }}import { ActivityIndicatorIOS } from "react-native";import { ActivityIndicatorIOS } from "react-native";/Button.ios.jsx:1:10 lint/nursery/useReactNativePlatformComponents ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ iOS component ActivityIndicatorIOS is used outside of an iOS-specific file.
> 1 │ import { ActivityIndicatorIOS } from “react-native”;
│ ^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Platform-specific components produce incorrect bundles when imported in shared files.
ℹ Move this import to a file with an iOS-specific suffix (e.g. .ios.js).
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.