Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.

Commit 1bce4df

Browse files
HomWangpi0
andauthored
docs(examples): add jsx example (#6870)
Co-authored-by: Pooya Parsa <[email protected]>
1 parent 91b4e6c commit 1bce4df

File tree

8 files changed

+83
-0
lines changed

8 files changed

+83
-0
lines changed

docs/content/2.guide/3.directory-structure/10.pages.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export default defineComponent({
3434
```
3535

3636
```ts [pages/index.tsx]
37+
// https://v3.nuxtjs.org/examples/advanced/jsx
3738
// https://vuejs.org/guide/extras/render-function.html#jsx-tsx
3839
export default defineComponent({
3940
render () {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
template: Example
3+
---
4+
5+
# JSX / TSX
6+
7+
This example shows how to use [jsx syntax](https://vuejs.org/guide/extras/render-function.html#jsx-tsx) with typescript in Nuxt pages and components.
8+
9+
::ReadMore{link="https://vuejs.org/guide/extras/render-function.html#jsx-tsx"}
10+
::
11+
12+
::sandbox{repo="nuxt/framework" branch="main" dir="examples/advanced/jsx" file="nuxt.config.ts"}

examples/advanced/jsx/app.vue

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<script lang="tsx" setup>
2+
// Component could be a simple function with JSX syntax
3+
const Welcome = () => <span>Welcome </span>
4+
5+
// Or using defineComponent setup that returns render function with JSX syntax
6+
const Nuxt3 = defineComponent(() => {
7+
return () => <span>nuxt3</span>
8+
})
9+
10+
// We can combine components with JSX syntax too
11+
const InlineComponent = () => (
12+
<div>
13+
<Welcome />
14+
<span>to </span>
15+
<Nuxt3 />
16+
</div>
17+
)
18+
</script>
19+
20+
<template>
21+
<NuxtExampleLayout example="advanced/jsx">
22+
<InlineComponent />
23+
<!-- Defined in components/jsx-component.ts -->
24+
<MyComponent message="This is an external JSX component" />
25+
</NuxtExampleLayout>
26+
</template>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default defineComponent({
2+
props: {
3+
message: String
4+
},
5+
render: (props) => {
6+
return (
7+
<div>
8+
{ props.message }
9+
</div>
10+
)
11+
}
12+
})

examples/advanced/jsx/nuxt.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineNuxtConfig } from 'nuxt'
2+
3+
export default defineNuxtConfig({
4+
modules: [
5+
'@nuxt/ui'
6+
]
7+
})

examples/advanced/jsx/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "example-jsx",
3+
"private": true,
4+
"scripts": {
5+
"build": "nuxi build",
6+
"dev": "nuxi dev",
7+
"start": "nuxi preview"
8+
},
9+
"devDependencies": {
10+
"@nuxt/ui": "^0.2.0",
11+
"nuxt": "npm:nuxt3@latest"
12+
}
13+
}

examples/advanced/jsx/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "./.nuxt/tsconfig.json"
3+
}

yarn.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6759,6 +6759,15 @@ __metadata:
67596759
languageName: unknown
67606760
linkType: soft
67616761

6762+
"example-jsx@workspace:examples/advanced/jsx":
6763+
version: 0.0.0-use.local
6764+
resolution: "example-jsx@workspace:examples/advanced/jsx"
6765+
dependencies:
6766+
"@nuxt/ui": ^0.2.0
6767+
nuxt: "npm:nuxt3@latest"
6768+
languageName: unknown
6769+
linkType: soft
6770+
67626771
"example-layouts@workspace:examples/routing/layouts":
67636772
version: 0.0.0-use.local
67646773
resolution: "example-layouts@workspace:examples/routing/layouts"

0 commit comments

Comments
 (0)