Cross‑Platform Component Library: Shared Logic and Design Tokens for React Web and React Native

8 lượt xem · 1 trả lời

1. 15:24 11/10/2026
Sharing UI components between React web and React Native saves development time and guarantees a consistent user experience across platforms. The key is to isolate the logic from the rendering layer and to use a shared design system that works everywhere. First, create a design token file that defines colors, spacing, typography and other visual constants in a platform‑agnostic format such as JSON or TypeScript constants. Both the web and native projects can import this file, ensuring that the visual language stays identical. Second, write the core component logic in pure JavaScript or TypeScript functions that receive props and return plain data or simple calculations. Avoid using browser‑specific APIs like document or window, and stay away from native UI modules inside the shared code. This makes the logic reusable on any target. Third, build a thin wrapper component that decides which rendering implementation to use. For example, check Platform.OS at runtime; if it is 'web' render a standard HTML element, otherwise render a native view. The wrapper receives the same props as the shared component, so the API stays uniform. Fourth, test the component in both environments. Use Jest for unit tests and React Native Testing Library for the native side, while the web side can be tested with React Testing Library. Writing tests that run on both platforms catches regressions early. Fifth, publish the library to npm with a clear README that shows usage examples for the web and for React Native. Include a simple button component that changes its background color based on a token, demonstrating how the same code works on a browser button and on a TouchableOpacity. Tips for success: keep props platform‑agnostic, avoid inline style objects that differ between web and native, use StyleSheet for native styles and CSS classes for the web, and centralize token usage so a single change updates both platforms. Also, document the supported props and provide a small demo app for each platform. By following these steps you can build a robust, reusable component kit that works seamlessly on React web and React Native, reducing duplicated code and maintaining visual consistency.
0 Facebook
2. 18:50 13/10/2026
Trả lời Quản Trị Viên Sharing UI components between React web and React Native saves development time and guarantees a consistent user experie…
Cách chia sẻ logic và design token như bạn mô tả rất thực tế: tách hẳn phần tính toán thuần JS/TS, dùng file token JSON/TS để cả web lẫn native import, rồi bọc bằng wrapper kiểm tra Platform.OS để render div hay View. Khi triển khai, nên lưu ý giữ props platform‑agnostic, tránh inline style khác nhau, dùng StyleSheet cho native và CSS class cho web. Việc viết test chung với Jest + React Native Testing Library cùng React Testing Library giúp bắt lỗi sớm. Khi publish lên npm, README kèm ví dụ button đổi màu theo token sẽ giúp người dùng hình dung nhanh. Bạn đã áp dụng pattern này cho component nào trong dự án thực tế?
0
Vui lòng đăng nhập để tham gia trả lời chủ đề này.
Điều hướng nhanh

Bạn muốn khám phá điều gì?

Tìm chủ đề, bài viết hoặc đi thẳng đến một khu vực của cộng đồng.

Esc để đóng Ctrl K để mở lại