forked from QwikDev/qwik
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Create initial tutorial pages (QwikDev#678) (QwikDev#694)
Co-authored-by: Miško Hevery <[email protected]>
- Loading branch information
1 parent
4fcb0ea
commit 6102668
Showing
126 changed files
with
3,101 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,5 +34,5 @@ | |
"Pinterest", | ||
"Serializability" | ||
], | ||
"enableFiletypes": ["!mdx"] | ||
"enableFiletypes": ["mdx"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
--- | ||
title: Components | ||
--- | ||
|
||
# Common Component Patterns Cheat Sheet | ||
|
||
## Declartion | ||
|
||
```tsx | ||
import {component$, useStore} from "@builder.io/qwik"; | ||
|
||
export const Greeter = component$(() => { | ||
return <span>Hello World!</span>; | ||
}); | ||
``` | ||
|
||
## Props | ||
|
||
```tsx | ||
import {component$, useStore} from "@builder.io/qwik"; | ||
|
||
interface GreeterProps { | ||
salutation?: string; | ||
name?: string; | ||
} | ||
export const Greeter = component$((props: GreeterProps) => { | ||
const salutation = props.salutation || 'Hello'; | ||
const name = props.name || 'World'; | ||
return <span>{salutation} {name}!</span>; | ||
}); | ||
``` | ||
|
||
### Event Props | ||
|
||
Component props must be serializable, and therefore can not directly reffer to functions. | ||
|
||
```tsx | ||
import {component$, useStore, Qrl} from "@builder.io/qwik"; | ||
|
||
export const Parent = component$(() => { | ||
return ( | ||
<MyButton doSomething$={() => console.log('Hello')}> | ||
click | ||
</MyButton> | ||
); | ||
}); | ||
|
||
interface MyButtonProps { | ||
doSomethingQrl: QRL<() => void> | ||
} | ||
export const MyButton = component$((props: MyButtonProps) => { | ||
return <button onClickQrl={props.doSomethingQrl}>click</button>; | ||
}); | ||
``` | ||
|
||
## Events | ||
|
||
## Watching for Changes | ||
|
||
## Server | ||
### Fetching Data | ||
|
||
```tsx | ||
import {component$, useStore, useServerMount$} from "@builder.io/qwik"; | ||
|
||
export const Greeter = component$(() => { | ||
const store = useStore<{list: null|string[]}>({list: null}); | ||
useServerMount$(async () => { | ||
store.list = await doSomethingToFetchDataOnServer(); | ||
}); | ||
|
||
return ( | ||
<ul> | ||
{store.list && store.list.map((item) => <li>{item}</li>)} | ||
</ul> | ||
); | ||
}); | ||
``` | ||
|
||
## Client | ||
### Eagerly Executing Code | ||
|
||
```tsx | ||
import {component$, useStore, useClientEffet} from "@builder.io/qwik"; | ||
|
||
export const Greeter = component$(() => { | ||
const store = useStore<{list: null|string[]}>({list: null}); | ||
useClientEffet$(async () => { | ||
store.list = await doSomethingToFetchDataOnServer(); | ||
}); | ||
|
||
return ( | ||
<ul> | ||
{store.list && store.list.map((item) => <li>{item}</li>)} | ||
</ul> | ||
); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- | ||
title: Serialization & Serialization Boundaries | ||
--- | ||
|
||
# `$` Boundaries | ||
|
||
## Rules | ||
|
||
- Only serializable data can cross a `$` boundery. | ||
|
||
## Serialization Boundery | ||
|
||
A serialization boundery occures whenever you cross a lexical scope of a function that is converted into lazy loadable form. It is always denoted by `$(...)` (or `____$(...)`) See example: | ||
|
||
```tsx | ||
import {component$} from "@builder.io/qwik"; | ||
|
||
export const topLevel = Promise.resolve('nonserializable data'); | ||
|
||
export const Greeter = component$(() => { | ||
// BEGIN component serialization boundery | ||
|
||
// Referring to top level symbols that are exported is always allowed. | ||
console.log(topLevel); // OK | ||
|
||
const captureSerializable = 'serializable data'; | ||
const captureNonSerializable = Promise.resolve('nonserializable data'); | ||
return ( | ||
<button onClick$={() => { | ||
// BEGIN onClick serialization boundery | ||
|
||
// Referring to top level symbols that are exported is always allowed. | ||
// Even if the value is non-serializable. | ||
console.log(topLevel); // OK | ||
|
||
// Capturing a non-toplevel variable is allowed only if: | ||
// - declaed as `const` | ||
// - is serializable (runtime error) | ||
console.log(captureSerializable); // OK | ||
|
||
// Reffering to captureNonSerializable will pass static analysis but | ||
// will fail at runtime because Qwik does not know how to serilize it. | ||
console.log(captureNonSerializable); // RUNTIME ERROR | ||
|
||
// END onClick serialization boundery | ||
}}> | ||
click | ||
</button> | ||
); | ||
// BEGIN component serialization boundery | ||
}); | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
title: Basic Component | ||
layout: tutorial | ||
--- | ||
|
||
Components are building blocks of Qwik application. Components are declared using `component$()` and at a minimum need to return a JSX. | ||
|
||
Create a component that returns `Hello World!` |
5 changes: 5 additions & 0 deletions
5
packages/docs/src/pages/tutorial/component/basic/problem/app.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { component$ } from '@builder.io/qwik'; | ||
|
||
export const App = component$(() => { | ||
return <span>__put_something_here__</span>; | ||
}); |
5 changes: 5 additions & 0 deletions
5
packages/docs/src/pages/tutorial/component/basic/solution/app.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { component$ } from '@builder.io/qwik'; | ||
|
||
export const App = component$(() => { | ||
return <span>Hello World!</span>; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
title: Binding Expressions | ||
layout: tutorial | ||
--- | ||
|
||
The purpose of components is to merge data with the template (JSX.) This is done with the help of the `{expression}` syntax and can be placed either as a text node or attribute. | ||
|
||
- Bind `data.name` to the `value` attribute of `<input/>`. | ||
- Bind `data.description` to the content of `<textarea/>`. |
26 changes: 26 additions & 0 deletions
26
packages/docs/src/pages/tutorial/component/binding/problem/app.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { component$ } from '@builder.io/qwik'; | ||
|
||
export const App = component$(() => { | ||
// @ts-ignore | ||
const data = { | ||
name: 'Qwik', | ||
description: DESCRIPTION, | ||
}; | ||
|
||
return ( | ||
<> | ||
<input value="data.name should go here" /> | ||
<br /> | ||
<textarea rows={10} cols={60}> | ||
data.description should go here | ||
</textarea> | ||
</> | ||
); | ||
}); | ||
|
||
export const DESCRIPTION = ` | ||
Qwik is designed for the fastest possible page load time, | ||
by delivering pure HTML with near 0 JavaScript for your | ||
pages to become interactive, regardless of how complex | ||
your site or app is. It achieves this via resumability | ||
of code.`; |
25 changes: 25 additions & 0 deletions
25
packages/docs/src/pages/tutorial/component/binding/solution/app.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { component$ } from '@builder.io/qwik'; | ||
|
||
export const App = component$(() => { | ||
const data = { | ||
name: 'Qwik', | ||
description: DESCRIPTION, | ||
}; | ||
|
||
return ( | ||
<> | ||
<input value={data.name} /> | ||
<br /> | ||
<textarea rows={10} cols={60}> | ||
{data.description} | ||
</textarea> | ||
</> | ||
); | ||
}); | ||
|
||
export const DESCRIPTION = ` | ||
Qwik is designed for the fastest possible page load time, | ||
by delivering pure HTML with near 0 JavaScript for your | ||
pages to become interactive, regardless of how complex | ||
your site or app is. It achieves this via resumability | ||
of code.`; |
8 changes: 8 additions & 0 deletions
8
packages/docs/src/pages/tutorial/component/composition/index.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
title: Component Composition | ||
layout: tutorial | ||
--- | ||
|
||
Components can be composed together to build applications. | ||
|
||
In this example we have pre-declared an `<App>` and a `<Greeter>` component. Place the `<Greeter>` component inside the `<App>` component so that the user can see its contents. |
Oops, something went wrong.