百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

reactivue

> 前端框架
开源

在 React 组件中使用 Vue Composition API

1.5K stars0 点赞1 次浏览
访问官网GitHub

工具介绍

在 React 组件中使用 Vue Composition API

在 React 组件中使用 Vue Composition API

bash
npm i reactivue

我非常喜欢 Vue Composition API 和其 反应性系统,
但 React 中的 函数式组件也与 TypeScript 相得益彰。
为什么不将它们结合使用呢?


使用方法

…

钩子工厂

为了重用组合逻辑,createSetup 提供了一个工厂,用于创建自己的钩子。

typescript
// mySetup.ts
import { createSetup, ref, computed, onUnmounted } from 'reactivue'

export interface Props {
  value: number
}

// create a custom hook that can be reused
export const useMySetup = createSetup(
  (props: Props) => {
    const counter = ref(props.value)
    const doubled = computed(() => counter.value * 2)
    const inc = () => counter.value += 1

    onUnmounted(() => console.log('Goodbye World'))

    return { counter, doubled, inc }
  },
)
typescript
// Counter.tsx
import React from 'react'
import { useMySetup, Props } from './mySetup'

export const Counter = (props: Props) => {
  const { counter, doubled, inc } = useMySetup(props)
  const { counter: counter2, doubled: doubled2, inc: inc2 } = useMySetup({ value: 10 })

  return (
    

      <button onClick={inc}>Increase</button>
      <br/>

      

      <button onClick={inc2}>Increase</button>
    </div>
  )
}

与 Preact 结合使用

要在 Preact 应用中使用 reactivue,只需将 reactivue 导入替换为 reactivue/preact

import { h } from 'preact'
- 导入 { defineComponent, ref, computed, onUnmounted } 从 'React'

<a href="https://www.npmjs.com/package/reactivue"></a>
<a href="https://bundlephobia.com/result?p=reactivue@latest"></a>

Issues· 27 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptreactvuevue-composition-apivue-in-react

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架