const vnode = (
h(
'div',
{
id: 'foo',
class: 'bar',
// here may go other props, possibly complex ones
},
[
/* children */
]
)
);
return (
<div class="container">
<div class="header">
Header content
{!isCompact && vnode}
</div>
<div class="footer">
Footer content
{isCompact && vnode}
</div>
</div>
);
npm install @vitejs/plugin-vue-jsx -D
// vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
export default defineConfig({
plugins: [
vue(),
vueJsx(),
],
// other configurations
...
});
// tsconfig.json
{
"compilerOptions": {
"jsx": "preserve",
"jsxImportSource": "vue",
// other compiler options
...
}
}
import { ref, h } from 'vue'
export default {
props: {
// define props here
...
},
setup(props) {
const count = ref(1)
// return the render function
return () => h('div', props.msg + count.value)
}
}
// Component.jsx
import { ref } from "vue"
export default {
setup(props) {
const count = ref(1)
// render function with JSX
return () => (
<div>
{props.msg + count.value}
</div>
)
}
}
// Component.tsx
import { defineComponent, provide, inject, Ref, ref } from "vue"
export default defineComponent({
setup(props) {
const count = ref(1)
// render function with JSX
return () => (
<div>
{props.msg + count.value}
</div>
)
}
})
// Component.vue
<script lang="tsx">
import { defineComponent } from 'vue'
export default defineComponent({
setup(props) {
return () => (
<div>
Hello, World
</div>
)
}
})
</script>
// Component.vue
<script lang="tsx" setup>
const RenderComponent = () => (
<div>
Hello, World
</div>
);
</script>
<template>
<RenderComponent />
</template>
// Component.vue
<script lang="tsx" setup>
import { ref } from 'vue';
const ReusedPart = () => (
<div>
Hello, World
</div>
);
const someCondition = ref(true);
</script>
<template>
<div>
<ReusedPart v-if="someCondition" />
</div>
<div>
<ReusedPart v-if="!someCondition" />
</div>
</template>
// Component.vue
<script lang="tsx" setup>
interface Props {
label: string;
}
const SubComponentA = (props: Props) => (
<div>
<label>{props.label}</label>
<input type="number" />
</div>
);
const SubComponentB = (props: Props) => (
<div>
<label>{props.label}</label>
<textarea />
</div>
);
</script>
<template>
<SubComponentA label="Label 1" />
<SubComponentB label="Label 2" />
<SubComponentA label="Label 3" />
<SubComponentB label="Label 4" />
</template>