react:
function Hello({ name }) {
return <h1>Hello {name}</h1>;
}
vue:
<script setup>
defineProps({ name: String })
</script>
<template>
<h1>Hello {{ name }}</h1>
</template>
react:
function Component({ title }) {
//...
}
vue:
<script setup>
const props = defineProps({ title: String })
</script>
react:
const [count, setCount] = useState(0)
vue:
<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>
react:
const double = useMemo(() => count * 2, [count])
vue:
<script setup>
const double = computed(() => count.value * 2)
</script>
vue:
react:
<button onClick={handleClick}>Click</button>
vue:
<button @click="handleClick">Click</button>
react:
// conditional render
{show && <Component />}
// list render
{items.map(item => <li key={item.id}>{item.label}</li>)}
vue:
// conditional render
<Component v-if="show" />
// list render
<li v-for="item in items" :key="item.id">
{{ item.label }}
</li>
react:
<input value={text} onChange={e => setText(e.target.value)} />
vue:
<input v-model="text" />
react:
// 父组件直接将方法传递给子组件调用
onSelect(value)
vue:
// 子组件 emit
<script setup>
const emit = defineEmits(['select'])
</script>
<button @click="emit('select', 123)">
Select
</button>
// 父组件 监听
<Child @select="handleSelect" />
(完)