Use Vue3 Composition Api in React to manage state, just import @vue/reactivity, rxv means React x Vue
Use all the reactive capabilities in @vue/reactivity in React apps
https://sl1673495.github.io/react-composition-api
@vue/reacivity
, using the full capacity Vue3 reactivity, with computed
, effect
other capabilities, and for Set
and Map
also provides a responsive ability. Follow-up will also become more complete and powerful with the update of this library.vue-next
Complete test cases inside the warehouse.@vue/reacivity
achieve super strong global state management capabilities .npm i rxv -S
npm i @vue/reactivity -S
Support all apis provided by @vue/reactivity and use them in React components.
store:
import { reactive } from "@vue/reactivity"
export const state = reactive({
count: 0,
})
export const add = () => (state.count += 1)
export const store = reactive(state)
export type Store = typeof store
Components:
import React from "react"
import { Provider, useStore } from "rxv"
import { store, add, Store } from "./store"
function Count() {
const countState = useStore((store: Store) => ({
count: store.count,
}))
return (
<Card hoverable style={{ marginBottom: 24 }}>
<h1>计数器</h1>
<div className="chunk">
<div className="text-chunk">
store中的count现在是 {countState.count}
</div>
<Button onClick={add}>add</Button>
</div>
</Card>
)
}
export default () => {
return (
<Provider value={store}>
<div className="flex">
<Count />
</div>
</Provider>
)
}
This example uses Vue3’s computed
, effect
etc. capabilities, which is a relatively complex example.
// store.ts
import { reactive, computed, effect } from "@vue/reactivity"
export const state = reactive({
count: 0,
})
const plusOne = computed(() => state.count + 1)
effect(() => {
console.log("plusOne changed: ", plusOne)
})
const add = () => (state.count += 1)
export const mutations = {
// mutation
add,
}
export const store = {
state,
computed: {
plusOne,
},
}
export type Store = typeof store
// Index.tsx
import { Provider, useStore } from "rxv"
function Count() {
const countState = useStore((store: Store) => {
const { state, computed } = store
const { count } = state
const { plusOne } = computed
return {
count,
plusOne,
}
})
return (
<Card hoverable style={{ marginBottom: 24 }}>
<h1>计数器</h1>
<div className="chunk">
<div className="chunk">store中的count现在是 {countState.count}</div>
<div className="chunk">
computed值中的plusOne现在是 {countState.plusOne.value}
</div>
<Button onClick={mutations.add}>add</Button>
</div>
</Card>
)
}
export default () => {
return (
<Provider value={store}>
<Count />
</Provider>
)
}
It can be seen that the definition of store completely reuses @vue/reactivity
the capabilities in it without introducing additional learning costs, and all capabilities in it can be perfectly supported.
Specific code and can see the effect of the document in 复杂示例
.
Except for several built-in APIs, all other APIs are provided internally by Vue.
The specific supported api, you can see vue-next
the exported api in the warehouse
export { ref, isRef, toRefs, Ref, UnwrapRef } from "./ref"
export {
reactive,
isReactive,
readonly,
isReadonly,
shallowReadonly,
toRaw,
markReadonly,
markNonReactive,
} from "./reactive"
export {
computed,
ComputedRef,
WritableComputedRef,
WritableComputedOptions,
ComputedGetter,
ComputedSetter,
} from "./computed"
export {
effect,
stop,
pauseTracking,
resumeTracking,
ITERATE_KEY,
ReactiveEffect,
ReactiveEffectOptions,
DebuggerEvent,
} from "./effect"
export { lock, unlock } from "./lock"
export { TrackOpTypes, TriggerOpTypes } from "./operations"
Note computed
, ref
the value of the package does not provide automatic unpacking these functions, you must use data.value
to read and assignments.
Author: sl1673495
Demo: https://sl1673495.github.io/react-composition-api/
Source Code: https://github.com/sl1673495/react-composition-api
#react #reactjs #javascript