你可以在 React 组件中直接使用小程序的自定义组件。包括支持原生 UI 组件库,如:weui, min-ali-ui等等
以阿里小程序的 Badge 组件为例:
import React from 'react';import { View } from 'remax/ali';import Badge from 'mini-ali-ui/es/badge'; // 直接当成 React 组件引用,无需申明 useComponentsexport default () => (<View><Badge><View slot="inner">Remax</View></Badge></View>);
使用项目中的自定义组件:
import React from 'react';import { View } from 'remax/ali';import NativeCard from './native-card'; // native-card 是一个原生自定义组件export default () => (<View><NativeCard /></View>);
以微信小程序的扩展组件库 weui 的 Cells\Cell 组件为例:
import * as React from 'react';import { View } from 'remax/wechat';import Cells from "weui-miniprogram/miniprogram_dist/cells/cells";import Cell from 'weui-miniprogram/miniprogram_dist/cell/cell';import 'weui-miniprogram/miniprogram_dist/weui-wxss/dist/style/weui.wxss';export default () => (<View><Cells title="带说明的列表项"><Cell value="标题文字" footer="说明文字"></Cell><Cell><View>标题文字(使用slot)</View><View slot="footer">说明文字</View></Cell></Cells></View>);
如果你使用的是 TypeScript,使用第三方小程序自定义组件时需要自己写组件类型,还是以 mini-ali-ui/es/badge
为例。
tsconfig.json
新增配置:{"compilerOptions": {"baseUrl": ".","paths": {"*": [ "./typings/*" ]},...}}
typings/min-ali-ui/es/badge.d.ts
文件import { ComponentType } from 'react';declare const Badge: ComponentType<{className?: string;text?: string;dot?: boolean;overflowCount?: number;withArrow?: boolean;direction?: 'middle' | 'left' | 'right';stroke?: boolean;}>;export default Badge;
在 typings/index.d.ts
写入以下内容:
declare module 'mini-ali-ui/es/badge';
错误:
import React from 'react';import { View } from 'remax/ali';import VantIcon from '@vant/weapp/lib/icon';export default () => {const handleClick = () => {};return (<View>{/** vant-weapp 中 icon 的属性定义为 class-prefix, bindclick,所以应遵循其命名规则 */}<VantIcon name="close" classPrefix="custom-class-prefix" onClick={handleClick} /></View>);};
正确:
import React from 'react';import { View } from 'remax/ali';import VantIcon from '@vant/weapp/lib/icon';export default () => {const handleClick = () => {};return (<View><VantIcon name="close" class-prefix="custom-class-prefix" bindclick={handleClick} /></View>);};
slot
的组件,具名 slot
部分的最外层只能用 View
组件。错误:
import React from 'react';import { View } from 'remax/ali';import Badge from 'mini-ali-ui/es/badge';export default () => (<View><Badge><Text slot="inner">Remax</Text></Badge></View>);
正确:
import React from 'react';import { View } from 'remax/ali';import Badge from 'mini-ali-ui/es/badge';export default () => (<View><Badge><View slot="inner">Remax</View></Badge></View>);
错误:
import React from 'react';import { View } from 'remax/ali';import Badge from 'mini-ali-ui/es/badge';export default () => {const badgeProps = {text: 1,};return (<View><Badge {...badgeProps}><View slot="inner">Remax</View></Badge></View>);};
正确:
import React from 'react';import { View } from 'remax/ali';import Badge from 'mini-ali-ui/es/badge';export default () => {return (<View><Badge text={1}><View slot="inner">Remax</View></Badge></View>);};