[React Native] Writing Platform-Specific Components for iOS and Android in React Native
2023-09-14 09:00:52 时间
Learn to write components that render differently on iOS and Android, but present the same API. First, we'll use the Platform module to change behavior based on the platform. Then, we'll use the platform-specific file extensions, .ios.js and .android.js, to render platform-specific components.
There are two ways to code for different platform.
1. Using 'Platform' from 'react-native' package.
import {Platform} from 'react-native'; ... render() { if(Platform.OS === 'ios') { let info = <Text>From ios</Text> } else if(Platform.OS === 'android'){ let info = <Text>From Android</Text> } }
This way works, but really not a good approach.
2. Using .ios & .android as file extention:
Create two files 'info.android.js' & 'info.ios.js':
// info.ios.js import React from 'react'; import {Text} from 'react-native'; export const PInfo = () => ( <Text>iOS</Text> ); // info.android.js import React from 'react'; import {Text} from 'react-native'; export const PInfo = () => ( <Text>Android</Text> );
Then we can use those component by:
// Import the file without extension import {PInfo} from './info'; // Use it as normal <PInfo />
相关文章
- Android入门基础教程(小白速成1)
- android中的加密算法,Android中加密算法[通俗易懂]
- Android StrictMode 详解
- Android源码学习之六——ActivityManager框架解析
- delphixe5 android,Delphi XE5 Android手机端转换Ansi字符串
- android scaleanimation动画,Android 的ScaleAnimation 缩放动画基本运用
- android系统中toast是什么_Android个人资料简单布局
- Android preference_android studio preview
- Android Socket通讯 之 心跳消息
- android scaleanimation动画,Android 动画之ScaleAnimation应用详解「建议收藏」
- Android resource linking failed_android sdk location should not
- android跳转到相册需要权限,Android打开相册获取图片路径[通俗易懂]
- Android修改字体_android设置字体样式
- 解决第三方邮箱APP登陆QQ、163邮箱无法验证账户名或密码的问题(IOS、MacOS、Windows、Android)
- PHP获取客户端机型,可区分windows、ios、android
- React Native学习笔记(一)—— Win11安卓子系统的安装与使用 - Windows Subsystem for Android - WSA
- 【错误记录】Android Studio 编译报错 ( Could not find com.android.tools.build:gradle:4.2.1. )
- 【Android 逆向】Android 权限 ( Android 逆向中使用的 android.permission 权限 | Android 系统中的 Linux 用户权限 )
- 【Android Gradle 插件】Gradle 依赖管理 ① ( org.gradle.api.Project 配置 | Android Gradle 插件配置与 Gradle 配置关联 ) ★
- Android剪裁图片简单的方法详解手机开发
- [android] ndk环境的搭建详解手机开发
- [android] 安卓消息推送的几种实现方式详解手机开发
- [android] xml文件的序列化详解手机开发
- android代码混淆详解手机开发
- Android热补丁技术—dexposed原理简析(手机淘宝采用方案)详解手机开发
- 操作系统比较:iOS 与 Linux 操作系统(ios与linux)
- 应用Linux上编译IOS应用:实现跨平台体验(linux编译ios)
- Zerodium:Android 漏洞的利用价值首次超越了 iOS
- 浅谈Android开放网络端口的安全风险
- 探秘iOS:它究竟是一种基于Linux的操作系统吗?(ios是linux系统吗)
- 为什么Android的图片质量没有iOS的高?
- Android自定义格式显示Button的布局思路
- Android笔记之:App自动化之使用Ant编译项目多渠道打包的使用详解
- Android日期时间格式国际化的实现代码
- Android和IOS的浏览器中检测是否安装某个客户端的方法