Platforma
Ta strona została przetłumaczona przez PageTurner AI (beta). Nie jest oficjalnie zatwierdzona przez projekt. Znalazłeś błąd? Zgłoś problem →
Przykład
Dokumentacja
Zatrzymuje działającą animację i resetuje wartość do oryginalnej.
constants
static constants: PlatformConstants;
Zwraca obiekt zawierający wszystkie dostępne stałe wspólne i specyficzne dla platformy.
Właściwości:
Name | Type | Optional | Description |
|---|---|---|---|
| isTesting | boolean | No | |
| reactNativeVersion | object | No | Information about React Native version. Keys are major, minor, patch with optional prerelease and values are numbers. |
| Version Android | number | No | OS version constant specific to Android. |
| Release Android | string | No | |
| Serial Android | string | No | Hardware serial number of an Android device. |
| Fingerprint Android | string | No | A string that uniquely identifies the build. |
| Model Android | string | No | The end-user-visible name for the Android device. |
| Brand Android | string | No | The consumer-visible brand with which the product/hardware will be associated. |
| Manufacturer Android | string | No | The manufacturer of the Android device. |
| ServerHost Android | string | Yes | |
| uiMode Android | string | No | Possible values are: 'car', 'desk', 'normal','tv', 'watch' and 'unknown'. Read more about Android ModeType. |
| forceTouchAvailable iOS | boolean | No | Indicate the availability of 3D Touch on a device. |
| interfaceIdiom iOS | string | No | The interface type for the device. Read more about UIUserInterfaceIdiom. |
| osVersion iOS | string | No | OS version constant specific to iOS. |
| systemName iOS | string | No | OS name constant specific to iOS. |
isPad iOS
static isPad: boolean;
Zwraca wartość logiczną określającą, czy urządzenie jest iPadem.
| Type |
|---|
| boolean |
isTV
static isTV: boolean;
Zwraca wartość logiczną określającą, czy urządzenie jest telewizorem.
| Type |
|---|
| boolean |
isVision
static isVision: boolean;
Zwraca wartość logiczną określającą, czy urządzenie jest Apple Vision. Jeśli używasz Apple Vision Pro („Designed for iPad”) isVision będzie miała wartość false, ale isPad będzie true
| Type |
|---|
| boolean |
isTesting
static isTesting: boolean;
Zwraca wartość logiczną określającą, czy aplikacja działa w trybie deweloperskim z ustawioną flagą testowania.
| Type |
|---|
| boolean |
OS
static OS: 'android' | 'ios';
Zwraca wartość typu string reprezentującą bieżący system operacyjny.
| Type |
|---|
enum('android', 'ios') |
Version
static Version: 'number' | 'string';
Zwraca wersję systemu operacyjnego.
| Type |
|---|
| number Android string iOS |
Metody
select()
static select(config: Record<string, T>): T;
Zwraca najbardziej odpowiednią wartość dla platformy, na której aktualnie działa aplikacja.
Parametry:
| Name | Type | Required | Description |
|---|---|---|---|
| config | object | Yes | See config description below. |
Metoda select zwraca najbardziej odpowiednią wartość dla bieżącej platformy. Dla urządzeń mobilnych pierwszeństwo mają klucze android i ios. Jeśli nie są zdefiniowane, używany jest klucz native, a następnie default.
Parametr config jest obiektem z następującymi kluczami:
-
android(dowolny) -
ios(dowolny) -
native(dowolny) -
default(dowolny)
Przykład użycia:
import {Platform, StyleSheet} from 'react-native';
const styles = StyleSheet.create({
container: {
flex: 1,
...Platform.select({
android: {
backgroundColor: 'green',
},
ios: {
backgroundColor: 'red',
},
default: {
// other platforms, web for example
backgroundColor: 'blue',
},
}),
},
});
Efektem będzie kontener z flex: 1 na wszystkich platformach, zielonym tłem na Androidzie, czerwonym na iOS oraz niebieskim na pozostałych platformach.
Ponieważ wartość klucza platformy może być typu any, metodę select można użyć do zwracania komponentów specyficznych dla platformy, jak poniżej:
const Component = Platform.select({
ios: () => require('ComponentIOS'),
android: () => require('ComponentAndroid'),
})();
<Component />;
const Component = Platform.select({
native: () => require('ComponentForNative'),
default: () => require('ComponentForWeb'),
})();
<Component />;