Przejdź do treści głównej
Wersja: 0.77

Platforma

Nieoficjalne Tłumaczenie Beta

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

tsx
static constants: PlatformConstants;

Zwraca obiekt zawierający wszystkie dostępne stałe wspólne i specyficzne dla platformy.

Właściwości:

Name
TypeOptionalDescription
isTestingbooleanNo
reactNativeVersionobjectNoInformation about React Native version. Keys are major, minor, patch with optional prerelease and values are numbers.
Version
Android
numberNoOS version constant specific to Android.
Release
Android
stringNo
Serial
Android
stringNoHardware serial number of an Android device.
Fingerprint
Android
stringNoA string that uniquely identifies the build.
Model
Android
stringNoThe end-user-visible name for the Android device.
Brand
Android
stringNoThe consumer-visible brand with which the product/hardware will be associated.
Manufacturer
Android
stringNoThe manufacturer of the Android device.
ServerHost
Android
stringYes
uiMode
Android
stringNoPossible values are: 'car', 'desk', 'normal','tv', 'watch' and 'unknown'. Read more about Android ModeType.
forceTouchAvailable
iOS
booleanNoIndicate the availability of 3D Touch on a device.
interfaceIdiom
iOS
stringNoThe interface type for the device. Read more about UIUserInterfaceIdiom.
osVersion
iOS
stringNoOS version constant specific to iOS.
systemName
iOS
stringNoOS name constant specific to iOS.

isPad
iOS

tsx
static isPad: boolean;

Zwraca wartość logiczną określającą, czy urządzenie jest iPadem.

Type
boolean

isTV

tsx
static isTV: boolean;

Zwraca wartość logiczną określającą, czy urządzenie jest telewizorem.

Type
boolean

isVision

tsx
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

tsx
static isTesting: boolean;

Zwraca wartość logiczną określającą, czy aplikacja działa w trybie deweloperskim z ustawioną flagą testowania.

Type
boolean

OS

tsx
static OS: 'android' | 'ios';

Zwraca wartość typu string reprezentującą bieżący system operacyjny.

Type
enum('android', 'ios')

Version

tsx
static Version: 'number' | 'string';

Zwraca wersję systemu operacyjnego.

Type
number
Android

string
iOS

Metody

select()

tsx
static select(config: Record<string, T>): T;

Zwraca najbardziej odpowiednią wartość dla platformy, na której aktualnie działa aplikacja.

Parametry:

NameTypeRequiredDescription
configobjectYesSee 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:

tsx
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:

tsx
const Component = Platform.select({
ios: () => require('ComponentIOS'),
android: () => require('ComponentAndroid'),
})();

<Component />;
tsx
const Component = Platform.select({
native: () => require('ComponentForNative'),
default: () => require('ComponentForWeb'),
})();

<Component />;