Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions docs-vitepress/guide/platform/rn.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,10 @@ env() 函数通过和 var() 函数类似形式, 区别在于:一是环境变
### 使用RN组件
在Mpx组件内引用RN组件,采用如下方式
- **RN组件注册方式**:需在components属性下进行引用注册。
- **RN组件的属性与事件**:属性与事件参考RN原生支持的属性与事件名,对应赋值方式按照Mpx语法进行双括号包裹,组件使用的值需要通过 REACTHOOKSEXEC方法的返回值的方式进行声明
- **RN组件的属性与事件**:属性与事件参考RN原生支持的属性与事件名,对应赋值方式按照Mpx语法进行双括号包裹,组件使用的值需要通过 onReactHooksExec 的返回值的方式进行声明
- **RN组件的样式定义**: 组件支持样式属性的透传,通过在RN组件上定义style即可透传样式
- **其他功能**: 支持在RN组件内使用slot
```javascript
```html
<template>
<view>
<!-- 事件的value需要使用双括号包裹 -->
Expand All @@ -328,17 +328,58 @@ env() 函数通过和 var() 函数类似形式, 区别在于:一是环境变
<view>我是Mpx组件</view>
<!-- 支持在RN组件内部定义插槽 -->
<slot name="myslot"></slot>
<View>
</View>
</ScrollView>
</view>
</template>
<script setup>
import { onReactHooksExec } from '@mpxjs/core'
import { ScrollView } from 'react-native-gesture-handler'
import { View } from 'react-native'

onReactHooksExec(() => {
return {
viewStyle: {
width: 200,
height: 200
}
}
})
defineOptions({
// 声明 RN 组件
components: {
ScrollView,
View
}
})
</script>
```

在选项式api中,使用 REACTHOOKSEXEC 替代 onReactHooksExec,例如
```html
<template>
<view>
<!-- 事件的value需要使用双括号包裹 -->
<ScrollView onScroll="{{scrollAction}}">
<View style="{{viewStyle}}">
<!-- 可混合编写mpx组件 -->
<view>我是Mpx组件</view>
<!-- 支持在RN组件内部定义插槽 -->
<slot name="myslot"></slot>
</View>
</ScrollView>
</view>
</template>
<script>
import { createComponent, REACTHOOKSEXEC } from '@mpxjs/core'
import { ScrollView } from 'react-native-gesture-handler'
import { View } from 'react-native'
createComponent({
// 声明 RN 组件
components: {
ScrollView
}
ScrollView,
View
},
[REACTHOOKSEXEC](){
return {
viewStyle: {
Expand All @@ -350,6 +391,9 @@ env() 函数通过和 var() 函数类似形式, 区别在于:一是环境变
})
</script>
```



### 使用React hooks
Mpx提供了hooks的执行机制,通过在Mpx组件内注册REACTHOOKSEXEC方法,保障RN组件的初始化执行。hooks的返回值支持数据与方法
- 模板上RN组件/Mpx组件的数据渲染
Expand Down
3 changes: 3 additions & 0 deletions packages/core/@types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ declare module '*?resolve' {
const resourcePath: string
export default resourcePath
}

declare let setAppShow: () => void
declare let setAppHide: () => void
43 changes: 16 additions & 27 deletions packages/core/@types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import type { GetComputedType } from '@mpxjs/store'
import type { ScaledSize } from 'react-native'
import type { ComponentType } from 'react'
export * from '@mpxjs/store'

// utils
Expand Down Expand Up @@ -126,9 +127,17 @@ interface Context {
type ExtendedComponentOptions = {
disconnectOnUnmounted?: boolean
shallowReactivePattern?: RegExp
/**
* 是否禁用render函数的useMemo,仅输出RN支持
*/
disableMemo?: boolean
} & WechatMiniprogram.Component.ComponentOptions

interface ComponentOpt<D extends Data, P extends Properties, C, M extends Methods, Mi extends Array<any>, S extends Record<any, any>> extends Partial<WechatMiniprogram.Component.Lifetimes & WechatMiniprogram.Component.OtherOption> {
/**
* ReactNative 原生组件注册
*/
components?: Record<string, ComponentType>,
data?: D
properties?: P
computed?: C
Expand Down Expand Up @@ -221,7 +230,7 @@ type WxComponentIns<D extends Data = {}, P extends Properties = {}, M extends Me
Omit<WechatMiniprogram.Component.Instance<D, P, M>, 'selectComponent' | 'selectAllComponents'>
& ReplaceWxComponentIns

type ComponentIns<D extends Data = {}, P extends Properties = {}, C = {}, M extends Methods = {}, Mi extends Array<any> = [], S extends Record<any, any> = {}, O = {}> =
export type ComponentIns<D extends Data = {}, P extends Properties = {}, C = {}, M extends Methods = {}, Mi extends Array<any> = [], S extends Record<any, any> = {}, O = {}> =
GetDataType<D> & UnboxMixinsField<Mi, 'data'> &
M & UnboxMixinsField<Mi, 'methods'> & { [K in keyof S]: S[K] extends Ref<infer V> ? V : S[K] } &
GetPropsType<P & UnboxMixinsField<Mi, 'properties'>> &
Expand Down Expand Up @@ -296,7 +305,7 @@ export interface RnConfig {
* - `true`:允许退出应用
* - `false`:阻止退出应用
*/
onAppBack?: () => boolean
onAppBack?: (delta: number) => boolean

/**
* 是否禁用框架内部的 AppStateChange 监听。
Expand Down Expand Up @@ -356,26 +365,6 @@ export interface RnConfig {
dimensions: T
) => T | void

/**
* 异步分包加载配置。
*/
asyncChunk?: {
/**
* 加载超时时长配置,单位为毫秒。
*/
timeout: number

/**
* 异步分包页面加载超时或失败时,自定义兜底页面文件路径。
*/
fallback: string

/**
* 异步分包页面加载时,自定义 loading 页面文件路径。
*/
loading: string
}

/**
* 加载并执行异步分包的方法。
*
Expand All @@ -399,10 +388,10 @@ interface MpxConfig {
ignoreWarning: boolean | string | RegExp | ((msg: string, location: string, e: Error) => boolean)
ignoreProxyWhiteList: Array<string>
observeClassInstance: boolean | Array<AnyConstructor>
errorHandler: (msg: String, location: String, e: Error) => any | null
warnHandler: (msg: String, location: String, e: Error) => any | null
proxyEventHandler: (e: WechatMiniprogram.CustomEvent, target: ComponentIns<{}, {}, {}, {}, []>) => any | null
setDataHandler: (data: object, target: ComponentIns<{}, {}, {}, {}, []>) => any | null
errorHandler: (msg: String, location: String, e: Error) => void
warnHandler: (msg: String, location: String, e: Error) => void
proxyEventHandler: (e: WechatMiniprogram.CustomEvent, target: ComponentIns<{}, {}, {}, {}, []>) => void
setDataHandler: (data: object, target: ComponentIns<{}, {}, {}, {}, []>) => void
forceFlushSync: boolean,
webRouteConfig: object,
webConfig: object,
Expand All @@ -413,7 +402,7 @@ interface MpxConfig {
*/
webviewConfig: WebviewConfig,
/** react-native 相关配置,用于挂载事件等,如 onShareAppMessage */
rnConfig?: RnConfig,
rnConfig: RnConfig,
}

type SupportedMode = 'wx' | 'ali' | 'qq' | 'swan' | 'tt' | 'web' | 'qa'
Expand Down
4 changes: 4 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@
"optional": true
}
},
"devDependencies": {
"@types/react": "^18.2.79",
"react-native": "^0.74.5"
},
"publishConfig": {
"registry": "https://registry.npmjs.org",
"access": "public"
Expand Down
8 changes: 4 additions & 4 deletions packages/store/@types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type UnboxDepField<D, F> = F extends keyof D ? D[F] : {}

type GetReturnOrSelf<T> = T extends (...args: any)=> infer R ? R : T

interface compContext {
export interface compContext {
[key: string]: any
}

Expand Down Expand Up @@ -158,7 +158,7 @@ type UnionToIntersection<U> = (U extends any
? I
: never;

interface mapStateFunctionType<S, G> {
export interface mapStateFunctionType<S, G> {
[key: string]: (state: S, getter: G) => any
}
interface DeeperMutationsAndActions {
Expand Down Expand Up @@ -242,7 +242,7 @@ interface StoreOptWithThis<S, G, M, A, D extends Deps> {
modules?: Record<string, StoreOptWithThis<{}, {}, {}, {}, {}>>
}

interface IStoreWithThis<S = {}, G = {}, M = {}, A = {}, D extends Deps = {}> {
export interface IStoreWithThis<S = {}, G = {}, M = {}, A = {}, D extends Deps = {}> {

[DEPS_SYMBOL]: D
[STATE_SYMBOL]: S
Expand Down Expand Up @@ -384,7 +384,7 @@ interface IStoreWithThis<S = {}, G = {}, M = {}, A = {}, D extends Deps = {}> {
mapActionsToInstance<T extends { [key: string]: string }>(obj: T, context: compContext): void
}

type StoreWithThis<S = {}, G = {}, M = {}, A = {}, D extends Deps = {}> = IStoreWithThis<S, G, M, A, D> & CompatibleDispatch
export type StoreWithThis<S = {}, G = {}, M = {}, A = {}, D extends Deps = {}> = IStoreWithThis<S, G, M, A, D> & CompatibleDispatch

interface StoreOpt<S, G, M, A, D extends Deps> {
state?: S,
Expand Down