Architecture & Platform Differences
Understand the fundamental differences between React Web and React Native
While React Web and React Native share the same core principles and syntax, they target different platforms with unique architectures, rendering engines, and capabilities.
React Web
1// React Web - DOM-based rendering2import React, { useState } from 'react';3import './styles.css';45// Web Component using HTML elements and CSS6const WebComponent = () => {7 const [isHovered, setIsHovered] = useState(false);8 const [clickCount, setClickCount] = useState(0);910 return (11 <div className="container">12 <h1 className="title">React Web Application</h1>1314 {/* HTML Elements */}15 <div16 className="card"17 onMouseEnter={() => setIsHovered(true)}18 onMouseLeave={() => setIsHovered(false)}19 style={{20 transform: isHovered ? 'scale(1.05)' : 'scale(1)',21 transition: 'transform 0.3s ease'22 }}23 >24 <img25 src="/image.jpg"26 alt="Web image"27 className="card-image"28 />29 <p className="card-text">30 This runs in a web browser using the DOM31 </p>32 </div>3334 {/* Web-specific APIs */}35 <button36 onClick={() => {37 setClickCount(prev => prev + 1);38 // Web APIs39 window.localStorage.setItem('clicks', String(clickCount + 1));40 document.title = `Clicked ${clickCount + 1} times`;41 }}42 className="button"43 >44 Click me (Web): {clickCount}45 </button>4647 {/* CSS Animations */}48 <div className="animated-box">49 CSS animations work natively50 </div>5152 {/* Web-only features */}53 <iframe54 src="https://example.com"55 width="100%"56 height="300"57 />5859 <input60 type="file"61 accept="image/*"62 onChange={(e) => console.log(e.target.files)}63 />64 </div>65 );66};6768// CSS file (styles.css)69/*70.container {71 max-width: 1200px;72 margin: 0 auto;73 padding: 20px;74}7576.card {77 background: white;78 border-radius: 8px;79 box-shadow: 0 2px 8px rgba(0,0,0,0.1);80 padding: 16px;81 cursor: pointer;82}8384.card:hover {85 box-shadow: 0 4px 16px rgba(0,0,0,0.15);86}8788.animated-box {89 animation: pulse 2s infinite;90}9192@keyframes pulse {93 0% { opacity: 1; }94 50% { opacity: 0.5; }95 100% { opacity: 1; }96}9798@media (max-width: 768px) {99 .container {100 padding: 10px;101 }102}103*/
React Native
1// React Native - Native component rendering2import React, { useState, useRef } from 'react';3import {4 View,5 Text,6 StyleSheet,7 TouchableOpacity,8 Image,9 ScrollView,10 Animated,11 Platform,12 Dimensions,13 Alert14} from 'react-native';15import AsyncStorage from '@react-native-async-storage/async-storage';1617// Native Component using Native elements18const NativeComponent = () => {19 const [clickCount, setClickCount] = useState(0);20 const scaleAnim = useRef(new Animated.Value(1)).current;21 const { width } = Dimensions.get('window');2223 const handlePress = () => {24 // Haptic feedback (iOS)25 if (Platform.OS === 'ios') {26 // Would use react-native-haptic-feedback27 }2829 // Animate30 Animated.sequence([31 Animated.timing(scaleAnim, {32 toValue: 0.95,33 duration: 100,34 useNativeDriver: true35 }),36 Animated.timing(scaleAnim, {37 toValue: 1,38 duration: 100,39 useNativeDriver: true40 })41 ]).start();42 };4344 return (45 <ScrollView style={styles.container}>46 <Text style={styles.title}>React Native Application</Text>4748 {/* Native Components */}49 <Animated.View50 style={[51 styles.card,52 { transform: [{ scale: scaleAnim }] }53 ]}54 >55 <Image56 source={{ uri: 'https://example.com/image.jpg' }}57 style={styles.cardImage}58 resizeMode="cover"59 />60 <Text style={styles.cardText}>61 This runs on iOS and Android natively62 </Text>63 </Animated.View>6465 {/* Native-specific APIs */}66 <TouchableOpacity67 onPress={async () => {68 setClickCount(prev => prev + 1);69 // Native APIs70 await AsyncStorage.setItem('clicks', String(clickCount + 1));71 Alert.alert('Button Pressed', `Clicked ${clickCount + 1} times`);72 }}73 style={styles.button}74 activeOpacity={0.8}75 >76 <Text style={styles.buttonText}>77 Click me (Native): {clickCount}78 </Text>79 </TouchableOpacity>8081 {/* Platform-specific rendering */}82 <View style={styles.platformBox}>83 <Text style={styles.platformText}>84 Running on {Platform.OS} {Platform.Version}85 </Text>86 </View>8788 {/* Native-only features */}89 {Platform.OS === 'ios' && (90 <Text style={styles.iosOnly}>91 iOS-specific feature92 </Text>93 )}94 </ScrollView>95 );96};9798const styles = StyleSheet.create({99 container: {100 flex: 1,101 backgroundColor: '#f5f5f5'102 },103 title: {104 fontSize: 24,105 fontWeight: 'bold',106 textAlign: 'center',107 marginVertical: 20,108 color: '#333'109 },110 card: {111 backgroundColor: 'white',112 borderRadius: 8,113 padding: 16,114 marginHorizontal: 20,115 marginVertical: 10,116 // React Native shadow117 ...Platform.select({118 ios: {119 shadowColor: '#000',120 shadowOffset: { width: 0, height: 2 },121 shadowOpacity: 0.1,122 shadowRadius: 4123 },124 android: {125 elevation: 4126 }127 })128 },129 cardImage: {130 width: '100%',131 height: 200,132 borderRadius: 8,133 marginBottom: 10134 },135 button: {136 backgroundColor: '#007AFF',137 paddingHorizontal: 20,138 paddingVertical: 12,139 borderRadius: 8,140 marginHorizontal: 20,141 alignItems: 'center'142 },143 buttonText: {144 color: 'white',145 fontSize: 16,146 fontWeight: '600'147 },148 platformBox: {149 backgroundColor: '#e0e0e0',150 padding: 10,151 margin: 20,152 borderRadius: 4153 }154});