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

Commit

Permalink
docs(examples): add jsx example (#6870)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <[email protected]>
  • Loading branch information
HomWang and pi0 authored Aug 23, 2022
1 parent 91b4e6c commit 1bce4df
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/content/2.guide/3.directory-structure/10.pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default defineComponent({
```

```ts [pages/index.tsx]
// https://v3.nuxtjs.org/examples/advanced/jsx
// https://vuejs.org/guide/extras/render-function.html#jsx-tsx
export default defineComponent({
render () {
Expand Down
12 changes: 12 additions & 0 deletions docs/content/4.examples/6.advanced/jsx.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
template: Example
---

# JSX / TSX

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.

::ReadMore{link="https://vuejs.org/guide/extras/render-function.html#jsx-tsx"}
::

::sandbox{repo="nuxt/framework" branch="main" dir="examples/advanced/jsx" file="nuxt.config.ts"}
26 changes: 26 additions & 0 deletions examples/advanced/jsx/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<script lang="tsx" setup>
// Component could be a simple function with JSX syntax
const Welcome = () => <span>Welcome </span>
// Or using defineComponent setup that returns render function with JSX syntax
const Nuxt3 = defineComponent(() => {
return () => <span>nuxt3</span>
})
// We can combine components with JSX syntax too
const InlineComponent = () => (
<div>
<Welcome />
<span>to </span>
<Nuxt3 />
</div>
)
</script>

<template>
<NuxtExampleLayout example="advanced/jsx">
<InlineComponent />
<!-- Defined in components/jsx-component.ts -->
<MyComponent message="This is an external JSX component" />
</NuxtExampleLayout>
</template>
12 changes: 12 additions & 0 deletions examples/advanced/jsx/components/MyComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default defineComponent({
props: {
message: String
},
render: (props) => {
return (
<div>
{ props.message }
</div>
)
}
})
7 changes: 7 additions & 0 deletions examples/advanced/jsx/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineNuxtConfig } from 'nuxt'

export default defineNuxtConfig({
modules: [
'@nuxt/ui'
]
})
13 changes: 13 additions & 0 deletions examples/advanced/jsx/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "example-jsx",
"private": true,
"scripts": {
"build": "nuxi build",
"dev": "nuxi dev",
"start": "nuxi preview"
},
"devDependencies": {
"@nuxt/ui": "^0.2.0",
"nuxt": "npm:nuxt3@latest"
}
}
3 changes: 3 additions & 0 deletions examples/advanced/jsx/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./.nuxt/tsconfig.json"
}
9 changes: 9 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6759,6 +6759,15 @@ __metadata:
languageName: unknown
linkType: soft

"example-jsx@workspace:examples/advanced/jsx":
version: 0.0.0-use.local
resolution: "example-jsx@workspace:examples/advanced/jsx"
dependencies:
"@nuxt/ui": ^0.2.0
nuxt: "npm:nuxt3@latest"
languageName: unknown
linkType: soft

"example-layouts@workspace:examples/routing/layouts":
version: 0.0.0-use.local
resolution: "example-layouts@workspace:examples/routing/layouts"
Expand Down

0 comments on commit 1bce4df

Please sign in to comment.