Skip to content

Commit 0f73997

Browse files
committed
feat(react): useProps helper
1 parent 4cb8039 commit 0f73997

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/babel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export default ({
5757
}
5858

5959
const reactivue = {
60-
reactivue: ['defineComponent', 'onPropsUpdated'],
60+
reactivue: ['defineComponent', 'onPropsUpdated', 'useProps'],
6161
}
6262
const runtime = {
6363
'@vue/runtime-core': [

src/react.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { EffectScope, Ref, UnwrapNestedRefs, UnwrapRef } from '@vue/runtime-core'
22
// @ts-expect-error setCurrentInstance not exposed
3-
import { createHook, effectScope, isProxy, isRef, nextTick, provide, reactive, readonly, setCurrentInstance, unref, watch } from '@vue/runtime-core'
3+
import { createHook, effectScope, isProxy, isRef, nextTick, provide, reactive, readonly, ref, setCurrentInstance, unref, watch } from '@vue/runtime-core'
44
import { Fragment, createElement, useEffect, useRef, useState } from 'react'
55

66
import type { ReactivueInternalInstance } from './shared'
@@ -120,10 +120,17 @@ export function createSetup<PropsType, State>(
120120
}
121121
}
122122
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-constraint
123-
export const onPropsUpdated = <Props extends any>(fn: (newProps: Props) => void) => {
123+
export const onPropsUpdated = <Props extends Record<any, any>>(fn: (newProps: Props) => void) => {
124124
createHook(LifecycleHooks.PROPS_UPDATED)(fn)
125125
}
126126

127+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-constraint
128+
export const useProps = <Props extends Record<any, any>>(props: Props) => {
129+
const reactiveProps = ref(props)
130+
onPropsUpdated<Props>(newProps => reactiveProps.value = newProps)
131+
return reactiveProps
132+
}
133+
127134
export function ReactivueProvider({ plugins, children }: { plugins?: any[]; children?: JSX.Element | JSX.Element[] }): JSX.Element {
128135
const scope = useRef<EffectScope | null>(null)
129136
if (scope.current === null)

0 commit comments

Comments
 (0)