DEV Community

Rex Pan
Rex Pan

Posted on • Edited on

From React to Vue Cheat-sheet

Dynamic component

const Component = isLogin ? AuthorizedPage : LoginPage;
(<Component />);
Enter fullscreen mode Exit fullscreen mode
<component :is="isLogin ? AuthorizedPage : LoginPage"></component>
Enter fullscreen mode Exit fullscreen mode

Document:

Fragment

(<>
    <h1 />
    <p />
</>)
Enter fullscreen mode Exit fullscreen mode
<template>
    <h1 />
    <p />
</template>
Enter fullscreen mode Exit fullscreen mode

useEffect

// React
useEffect(f, xs)

// Vue
watch(() => xs, (o, n, onCleanup) => onCleanup(f()), { immediate:true })
watchEffect((onCleanUp) => onCleanUp(f()))
Enter fullscreen mode Exit fullscreen mode
// React
useEffect(function f() {
    /* use */ dependency1, dependency2;
    return function cleanUp() { /* clean up */ }
}, [dependency1, dependency2])

// Vue
watch(() => [dependency1, dependency2], (o, n, onCleanup) => {
    /* use */ dependency1, dependency2;
    onCleanup(function cleanUp() { /* clean up */ })
}, { immediate:true })
watchEffect((onCleanUp) => {
    /* use */ dependency1, dependency2;
    onCleanup(function cleanUp() { /* clean up */ })
})
Enter fullscreen mode Exit fullscreen mode

Portal

createPortal(<>...</>, portalDomElement)
Enter fullscreen mode Exit fullscreen mode
<teleport to="#portalDomId">...</teleport>
Enter fullscreen mode Exit fullscreen mode

Context

// React
import { createContext, useContext } from "react"
const MyContext = createContext()
function Ancestor() {
    return (<MyContext.Provider value={value}>...</MyContext>)
}
function Descendant() {
    const value = useContext(MyContext)
}

// Vue
import { provide, inject } from "vue"
// Ancestor.vue
provide("key", value)
// Descendant.vue
const value = inject("key")
Enter fullscreen mode Exit fullscreen mode

Ref

import { ref, onMounted } from 'vue'
const rInput = ref<HTMLInputElement | null>(null)

// ChildComponent.vue
defineExpose({ open })

// ParentComponent.vue
const rChild = ref<InstanceType<typeof ChildComponent>|null>(null)
rChild.value?.open()
Enter fullscreen mode Exit fullscreen mode

Lazy

import { lazy } from "react";
const LazyComponent = lazy(() => import("./Component"));
Enter fullscreen mode Exit fullscreen mode
import { defineAsyncComponent  } from "vue";
const LazyComponent = defineAsyncComponent(() => import("./Component"));
Enter fullscreen mode Exit fullscreen mode

Slot / children

Child.vue

<template>
    <slot :a="1" :b="2">
        fallback content
    </slot>

    <slot name="header" v-bind="headerSlotProps"></slot>
<template>
Enter fullscreen mode Exit fullscreen mode
  • Parent.vue
<template>
    <Child v-slot="defaultSlotProps">
        default slot content {{ defaultSlotProps.a }} {{ defaultSlotProps.b }}

        <template #default="defaultSlotProps">
            default slot content {{ defaultSlotProps.a }} {{ defaultSlotProps.b }}
        </template>

        <template #header="headerSlotProps">
            header slot content
        </template>
        <template #["header"]>
            header slot content
        </template>
        <template v-slot:["header"]>
            header slot content
        </template>
    <Child/>
<template>
Enter fullscreen mode Exit fullscreen mode

Suspense

Parent.vue

<script setup>
import { Suspense } from "vue"
</script>
<template>
    <Suspense>
        <Child />

        <template #fallback>
            loading...
        </template>
    </Suspense>
</template>
Enter fullscreen mode Exit fullscreen mode

Child.vue

<script setup>
const res = await fetch(...)
const posts = await res.json()
</script>

<template>
  {{ posts }}
</template>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)