SwiftUI 与 React Native
发布: (2025年12月26日 GMT+8 22:55)
2 min read
原文: Dev.to
Source: Dev.to
引言
我已经使用 SwiftUI 和 React Native 几年了,发现这两个声明式 UI 框架有很多相似之处,这一点在实现自定义视图时尤为明显。
示例:LocationCardView
SwiftUI
struct LocationCardView: View {
@Environment(Theme.self) private var theme
var locationCoordinate: LocationCoordinate
var body: some View {
VStack(alignment: .leading, spacing: theme.spacing.md) {
Text(locationCoordinate.city)
.font(.headline)
Text(locationCoordinate.state)
.font(.caption)
Text(locationCoordinate.countryCode)
.font(.caption)
}
.locationCardViewStyle(theme)
}
}
React Native (TypeScript)
const LocationCardView = (props: LocationCardViewProps) => {
const { location } = props
const { theme } = useThemeManager((state) => state)
const styles = createLocationCardViewStyles(theme, props)
return (
{location.city}
{location.state}
{location.countryCode}
)
}
观察
两种实现方式都需要相似的代码行数来完成相同的任务。然而,SwiftUI 开箱即用地提供了像 VStack 这样的基础原语,这可以减少内存开销和开发时间。就个人而言,我更喜欢 SwiftUI 的开发者体验。
讨论
你对这两个框架有什么看法?更倾向于哪一个?欢迎在评论中分享你的经验。