name: react-to-wx-miniprogram-migrator description: Migrates a React + TailwindCSS H5 web application to a native WeChat Mini Program. Use when the user wants to convert their existing web project into a mini program, preserving structure, styling, and functionality. license: MIT metadata: author: Manus AI version: "1.0"
React + TailwindCSS to WeChat Mini Program Migration Skill
This skill provides a systematic process for an AI agent to migrate a web application built with React and TailwindCSS into a native WeChat Mini Program. The goal is to achieve a functional and stylistically faithful conversion by mapping modern web development patterns to the specific architecture of WeChat Mini Programs.
When to Use This Skill
Use this skill when the user explicitly requests to convert a React-based H5 website, especially one using TailwindCSS for styling, into a native WeChat Mini Program. The source code of the React project should be available for analysis.
Migration Process Overview
The migration is a multi-step process that involves code transformation, style conversion, and API replacement. Follow these steps sequentially for a successful migration.
- Analyze Source & Setup Project: Analyze the React project structure and set up a corresponding WeChat Mini Program project.
- Component & JSX to WXML Conversion: Convert React components and JSX syntax into Mini Program components and WXML structure.
- TailwindCSS to WXSS Conversion: Transform TailwindCSS utility classes into equivalent WXSS styles.
- Logic & State Management Migration: Adapt React's state management (Hooks, Redux) and lifecycle to the Mini Program's
dataandsetDatasystem. - Routing & Navigation Replacement: Replace web-based routing (e.g., React Router) with the Mini Program's page navigation API.
- API & SDK Replacement: Substitute browser/web APIs (e.g.,
fetch,localStorage) with their WeChat Mini Program counterparts (wx.request,wx.setStorageSync). - Asset & Resource Handling: Migrate and correctly reference static assets like images and fonts.
- Final Review & Debugging: Thoroughly test the migrated application in the WeChat DevTools and on a physical device.
Step-by-Step Migration Instructions
Step 1: Analyze Source & Setup Project
- Analyze React Project: Use the
ls -Rortreecommand to inspect the source React project's directory structure. Identify the main components, pages, utility functions, and static assets. - Create Mini Program Structure: Based on the analysis, create a standard WeChat Mini Program directory structure. Refer to
references/MIGRATION_MAP.mdfor the standard structure.- Create the
app.js,app.json, andapp.wxssroot files. - Create a
pagesdirectory and subdirectories for each page identified in the React app. - Create a
componentsdirectory for reusable components. - Create a
utilsdirectory for helper functions.
- Create the
- Configure
app.json: Populate theapp.jsonfile. List all the pages in thepagesfield. If the H5 app has a tab bar, configure thetabBarfield accordingly.
Step 2: Component & JSX to WXML Conversion
This is the core of the UI migration. Convert each React component into a Mini Program component folder containing .js, .wxml, .wxss, and .json files.
- Map HTML Tags to WXML Components: For each JSX file, systematically replace HTML tags with their corresponding WXML component equivalents. A detailed mapping can be found in
references/MIGRATION_MAP.md.div->viewspan,p,h1-h6->textimg->imagea->navigatorbutton->button
- Convert JSX Syntax: Transform JSX control flow and expressions into WXML directives.
- List Rendering: Convert
.map()calls towx:forloops. - Conditional Rendering: Convert
&&,? :operators towx:if,wx:elif,wx:elsedirectives. - Data Binding: Convert
{variable}to{{variable}}. - Event Handling: Convert
onClick={handler}tobindtap="handler".
- List Rendering: Convert
Step 3: TailwindCSS to WXSS Conversion
This is a critical and potentially complex step. The primary challenge is converting utility-first CSS into static WXSS stylesheets, as Mini Programs do not have a JIT compiler for styles.
- Identify All Tailwind Classes: Scan all JSX files and extract every TailwindCSS class name used.
- Convert Classes to CSS: Use an online tool or a script to convert the list of Tailwind classes into standard CSS. You can use this Tailwind to CSS Converter for this purpose.
- Adapt CSS for WXSS: Manually adjust the generated CSS to be compatible with WXSS.
- Convert Units: Change
pxorremunits torpx. A common baseline is1rem(16px) =32rpx. Use this as a general rule but adjust for precision. - Handle Unsupported Selectors: Replace unsupported selectors like the universal selector (
*) or complex descendant selectors. - Address
gapProperty: Sincegapis not supported, implement spacing between flex/grid items usingmarginon the child elements.
- Convert Units: Change
- Organize Styles: Paste the converted styles into the appropriate
.wxssfiles. Global styles (fromindex.cssorApp.css) go intoapp.wxss, and component-specific styles go into the component's.wxssfile.
Step 4: Logic & State Management Migration
- Component Logic: Move the logic from the React component function body into the
Page({...})orComponent({...})object in the.jsfile. - State Management: Refactor React state to the Mini Program's
dataobject.- Convert
useStatedeclarations to initial values in thedataobject. - Replace state update calls (e.g.,
setCount(1)) withthis.setData({ count: 1 }).
- Convert
- Lifecycle Methods: Map React's lifecycle hooks to Mini Program lifecycle methods. See
references/MIGRATION_MAP.mdfor a detailed mapping.useEffectwith an empty dependency array[]maps toonLoad.useEffectwith no dependency array maps roughly toonShow.- The return function from
useEffectmaps toonUnload.
Step 5: Routing & Navigation Replacement
Replace all instances of React Router or other routing libraries with the Mini Program's built-in navigation API.
<Link to="/path">ornavigate('/path')becomeswx.navigateTo({ url: '/pages/path/index' }).- Use
wx.switchTabfor navigating to tab bar pages. - Use
wx.redirectTofor redirects. - Use
wx.navigateBackfor going back.
Step 6: API & SDK Replacement
Replace all browser-specific APIs with their wx. counterparts.
- Network Requests: Replace
fetchoraxioswithwx.request. - Local Storage: Replace
localStoragewithwx.setStorageSyncandwx.getStorageSync. - DOM Manipulation: Remove all direct DOM manipulation code (e.g.,
document.getElementById). Instead, use data binding to control the view.
Step 7: Asset & Resource Handling
- Images: Move all image assets into a dedicated directory (e.g.,
/images). Update all<image>srcpaths to be absolute paths from the project root (e.g.,/images/logo.png). - Fonts: If custom fonts are used, they need to be loaded using
wx.loadFontFaceinapp.js.
Step 8: Final Review & Debugging
- Use WeChat DevTools: This is your primary environment for testing and debugging. Check the console for errors and use the WXML panel to inspect the UI structure.
- Test on Device: Preview the Mini Program on a physical device to check for performance issues and styling inconsistencies that may not appear in the simulator.
- Iterate and Refine: The migration process is iterative. Expect to go back and forth between steps to fix bugs and refine the user experience.
Bundled Resources
references/MIGRATION_MAP.md: Contains detailed mapping tables for components, events, lifecycles, and APIs to assist in the conversion process.