antfu/reactivue là dự án tập trung giao diện trên GitHub với 1.5k sao, viết chủ yếu bằng TypeScript. 🙊 Use Vue Composition API in React components
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
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
import React from 'React'
import { defineComponent, ref, computed, onUnmounted } from 'reactivue'
interface Props {
value: number
}
const MyCounter = defineComponent(
// setup function in Vue
(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 }
},
// functional component in React
({ counter, doubled, inc }) => {
// you can still use other React hooks
return (
<div>
<div>{counter} x 2 = {doubled}</div>
<button onClick={inc}>Increase</button>
</div>
)
}
)
// use it as you normally would
render(<MyCounter value={10}>, el)
Hooks
You can use it as a hook as well.
The defineComponent factory is actually a sugar to and equivalent to the following code.
import React from 'React'
import { useSetup, ref, computed, onUnmounted } from 'reactivue'
interface Props {
value: number
}
function MyCounter(Props: Props) {
const state = useSetup(
(props: Props) => { // props is a reactive object in Vue
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 }
},
Props // pass React props to it
)
// state is a plain object just like React state
const { counter, doubled, inc } = state
return (
<div>
<div>{counter} x 2 = {doubled}</div>
<button onClick={inc}>Increase</button>
</div>
)
}
Hook Factory
To reuse the composition logics, createSetup is provided as a factory to create your own hooks.
To use reactivue in Preact apps, just replace reactivue import with reactivue/preact
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.
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.
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 - 🍍 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
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.
// 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.
antfu/reactivue có 1.5k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/antfu/reactivue. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
antfu/reactivue có những chủ đề gì?
GitHub topics của antfu/reactivue: "react", "vue", "vue-composition-api", "vue-in-react". TopGit xếp repo vào nhóm Frontend.
antfu/reactivue có website riêng không?
TopGit chưa ghi nhận URL trang chủ cho antfu/reactivue. Phần README ở tab phía trên thường có link demo, hoặc xem mô tả GitHub của repo.
antfu/reactivue còn đang phát triển không?
Commit gần nhất trên antfu/reactivue là 2.2 năm trước (theo timestamp GitHub). Repo có 39 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
antfu/reactivue dùng license gì?
antfu/reactivue phát hành theo license MIT. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
antfu/reactivue viết bằng ngôn ngữ gì?
antfu/reactivue chủ yếu viết bằng TypeScript. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Đọc thêm về antfu/reactivue ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/antfu/reactivue là nguồn chính thức.
Đọc đầy đủ README ở tab phía trên.
reactivue có đáng để bạn bỏ thời gian?
ChatGPT, Claude và Perplexity đều đọc được trang này. Hỏi thử xem họ nghĩ gì về reactivue.