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

reactivue

> 前端框架
开源

在 React 组件中使用 Vue Composition API

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

工具介绍

在 React 组件中使用 Vue Composition API

Use Vue Composition API in React components

npm i reactivue

I love Vue Composition API and its reactivity system,
but functional components in React are also sweet with Typescript.
Instead of making a choice, why not to use them together?


## Usage ### Component Factory ``` … ``` ### Hooks You can use it as a hook as well. > The `defineComponent` factory is actually a sugar to and equivalent to the following code. ``` … ``` ### Hook Factory To reuse the composition logics, `createSetup` is provided as a factory to create your own hooks. ```ts // 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 } }, ) ``` ```tsx // 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 ( Increase
Increase ) } ``` ## Usage with Preact To use reactivue in Preact apps, just replace `reactivue` import with `reactivue/preact` ```diff import { h } from 'preact' -import { defineComponent, ref, computed, onUnmounted } from 'reactivue' +import { defineComponent, ref, computed, onUnmounted } from 'reactivue/preact' ``` ## Using Vue's Libraries *Yes, you can!* Before you start, you need set alias in your build tool in order to redirect some apis from `vue` to `reactivue` or `reactivue/preact` if you are using it with Preact. #### Aliasing Vite
Add following code to `vite.config.js` ```js { /* ... */ alias: { 'vue': 'reactivue', '@vue/runtime-dom': 'reactivue', } } ``` If you are using it with Preact you have to add following code to `vite.config.js` ```ts { /* ... */ optimizeDeps: { include: ['reactivue/preact'], exclude: ['@vue/reactivity'] } } ``` Webpack
Add following code to your webpack config ```js const config = { /* ... */ resolve: { alias: { 'vue': 'reactivue', '@vue/runtime-dom': 'reactivue', }, } } ``` Parcel
Parcel uses the standard `package.json` file to read configuration options under an `alias` key. ```js { "alias": { "vue": "reactivue", "@vue/runtime-dom": "reactivue", }, } ``` Rollup
To alias within Rollup, you'll need to install [@rollup/plugin-alias](https://github.com/rollup/plugins/tree/master/packages/alias). The plugin will need to be placed before your `@rollup/plugin-node-resolve`. ```js import alias from '@rollup/plugin-alias'; module.exports = { plugins: [ alias({ entries: [ { find: 'vue', replacement: 'reactivue' }, { find: '@vue/runtime-dom', replacement: 'reactivue' } ] }) ] }; ``` Jest
Jest allows the rewriting of module paths similar to bundlers. These rewrites are configured using regular expressions in your Jest configuration: ```js { "moduleNameMapper": { "^vue$": "reactivue", "^@vue/runtime-dom$": "reactivue", } } ``` #### Installing Vue Plugins Installing Vue plugins are almost identical to Vue. Just simply create your root instance with `createApp` function and register your plugins as you do in Vue apps. **You don't need to call `app.mount`**. Your Vue plugins will be available in all your setup functions. ```ts import { createApp } from 'reactivue' import { createPinia } from 'pinia' const app = createApp() app.use(createPinia()) ``` > Note: If you are trying to use a library that calls app.component, app.directive or app.mixin in its install function, reactivue will skip these calls without any action and warn you about it. #### Compatible Libraries > A list of libaries that have been tested to work with `reactivue`. Feel free to make PRs adding more. - [pinia](https://github.com/posva/pinia) - Automatically Typed, Modular and lightweight Store for Vue - [VueUse](https://github.com/vueuse/vueuse) - Collection of Composition API utils for Vue 2 and 3 - [Villus](https://github.com/logaretm/villus) - A tiny and fast GraphQL client for Vue.js ## APIs Some tips and cavert compare to Vue's Composition API. #### Reactivity The reactivity system APIs are direct re-exported from `@vue/reactivity`, they should work the same as in Vue. ````ts // the following two line are equivalent. import { ref, reactive, computed } from 'reactivue' import { ref, reactive, computed } from '@vue/reactivity' ```` #### Lifecycles This library implemented the basic lifecycles to bound with React's lifecycles. For some lifecycles that don't have the React equivalent, they will be called somewhere near when they should be called (for example `onMounted` will be call right after `onCreated`). For most of the time, you can use them like you would in Vue. #### Extra APIs - `defineComponent()` - not the one you expected to see in Vue. Instead, it accepts a setup function and a render function that will return a React Functional Component. - `useSetup()` - the hook for resolve Composition API's setup, refer to the section above. - `createSetup()` - a factory to wrapper your logics into reusable custom hooks. #### Limitations - `getCurrentInstance()` - returns the meta info for the internal states, NOT a Vue instance. It's exposed to allow you check if it's inside a instance scope. - `emit()` is not available ### Examples #### Real-world Examples/Showcases - [Café CN](https://github.com/antfu/awesome-cn-cafe-web) - Web App for Awesome CN Café - [Preact Browser Example](./examples/preact-htm-browser) - [React Vite 2.0 Demo](./examples/react-ts-vite) ### License [MIT License](https://github.com/antfu/rectivue/blob/master/LICENSE) © 2020 [Anthony Fu](https://github.com/antfu)

GitHub Issues· 27 开放

在 GitHub 查看全部
  • #74

    I made my own fork (Pruve)

    更新于 2026年5月27日
  • #73

    React19 cannot execute

    更新于 2025年3月28日
  • #70

    not working with react 18.2.0: Cannot read properties of undefined (reading 'stop')

    更新于 2024年1月29日
  • #71

    Not working in React16 Projects

    更新于 2023年9月7日
  • #65

    [Bug] Unable to Trigger View Render and Watch with reactivue 0.4.4

    更新于 2023年4月21日
  • #63

    Run examples/react-ts-vite will throw error

    更新于 2023年1月13日

核心特点

  • •pinia - Automatically Typed, Modular and lightweight Store for Vue
  • •VueUse - Collection of Composition API utils for Vue 2 and 3
  • •Villus - A tiny and fast GraphQL client for Vue.js
  • •defineComponent() - not the one you expected to see in Vue. Instead, it accepts a setup function and a render function that will return a React Functional Component.
  • •useSetup() - the hook for resolve Composition API's setup, refer to the section above.
  • •createSetup() - a factory to wrapper your logics into reusable custom hooks.
  • •getCurrentInstance() - returns the meta info for the internal states, NOT a Vue instance. It's exposed to allow you check if it's inside a instance scope.
  • •emit() is not available
  • •Café CN - Web App for Awesome CN Café
  • •Preact Browser Example

> 标签

TypeScriptreactvuevue-composition-apivue-in-react

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

> 工具信息

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

> 相关工具

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