Reactivity
Declare state
Qwik
import { component$, useSignal } from "@builder.io/qwik";
export const Name = component$(() => {
const name = useSignal("John");
return <h1>Hello {name.value}</h1>;
});
Update state
Qwik
import { component$, useTask$, useSignal } from "@builder.io/qwik";
export const Name = component$(() => {
const name = useSignal("John");
useTask$(() => {
name.value = "Jane";
});
return <h1>Hello {name.value}</h1>;
});
Computed state
Qwik
import { component$, useSignal, useComputed$ } from "@builder.io/qwik";
export const DoubleCount = component$(() => {
const count = useSignal(10);
const doubleCount = useComputed$(() => count.value * 2);
return <div>{doubleCount.value}</div>;
});
Templating
Minimal template
Styling
Qwik
import { component$, useStyles$ } from "@builder.io/qwik";
export const App = component$(() => {
useStyles$(`
.title {
color: red;
}
`);
return (
<>
<h1 class="title">I am red</h1>
<button style={{ "font-size": "10rem" }}>I am a button</button>
</>
);
});
Loop
Qwik
import { component$ } from "@builder.io/qwik";
export const Colors = component$(() => {
const colors = ["red", "green", "blue"];
return (
<ul>
{colors.map((color) => (
<li key={color}>{color}</li>
))}
</ul>
);
});
Event click
Qwik
import { component$, useSignal, $ } from "@builder.io/qwik";
export const Counter = component$(() => {
const count = useSignal(0);
const incrementCount = $(() => {
count.value++;
});
return (
<>
<p>Counter: {count.value}</p>
<button onClick$={incrementCount}>Increment</button>
</>
);
});
Dom ref
Qwik
import { component$, useVisibleTask$, useSignal } from "@builder.io/qwik";
export const InputFocused = component$(() => {
const inputElement = useSignal<HTMLInputElement>();
useVisibleTask$(({ track }) => {
const el = track(inputElement);
el?.focus();
});
return <input type="text" ref={inputElement} />;
});
Conditional
Qwik
import { $, component$, useComputed$, useSignal } from "@builder.io/qwik";
export const TRAFFIC_LIGHTS = ["red", "orange", "green"];
export const TrafficLight = component$(() => {
const lightIndex = useSignal(0);
const light = useComputed$(() => TRAFFIC_LIGHTS[lightIndex.value]);
const nextLight = $(() => {
lightIndex.value = (lightIndex.value + 1) % TRAFFIC_LIGHTS.length;
});
return (
<>
<button onClick$={nextLight}>Next light</button>
<p>Light is: {light.value}</p>
<p>
You must {light.value === "red" && <span>STOP</span>}
{light.value === "orange" && <span>SLOW DOWN</span>}
{light.value === "green" && <span>GO</span>}
</p>
</>
);
});
import { track } from "ripple";
const TRAFFIC_LIGHTS = ["red", "orange", "green"];
export default function TrafficLight() {
let &[lightIndex] = track(0);
let &[light] = track(() => TRAFFIC_LIGHTS[lightIndex]);
function nextLight() {
lightIndex = (lightIndex + 1) % TRAFFIC_LIGHTS.length;
}
return <>
<button onClick={nextLight}>"Next light"</button>
<p>"Light is "{light}</p>
<p>
"You must "
if (light === "red") {
<span>"STOP"</span>
} else if (light === "orange") {
<span>"SLOW DOWN"</span>
} else if (light === "green") {
<span>"GO"</span>
}
</p>
</>;
}
Lifecycle
On mount
Qwik
import { component$, useVisibleTask$, useStore } from "@builder.io/qwik";
export const App = component$(() => {
const store = useStore({
pageTitle: "",
});
useVisibleTask$(() => {
store.pageTitle = document.title;
});
return <p>Page title: {store.pageTitle}</p>;
});
On unmount
Qwik
import { component$, useVisibleTask$, useStore } from "@builder.io/qwik";
export const App = component$(() => {
const store = useStore({
time: new Date().toLocaleTimeString(),
});
useVisibleTask$(({ cleanup }) => {
const timer = setInterval(() => {
store.time = new Date().toLocaleTimeString();
}, 1000);
cleanup(() => clearInterval(timer));
});
return <p>Current time: {store.time}</p>;
});
import { effect, track } from "ripple";
export default function Time() {
let &[time] = track(new Date().toLocaleTimeString());
effect(() => {
const timer = setInterval(() => {
time = new Date().toLocaleTimeString();
}, 1000);
return () => clearInterval(timer);
});
return <h1>"Current time: "{time}</h1>;
}
Component composition
Props
Qwik
import { component$ } from "@builder.io/qwik";
import UserProfile from "./UserProfile";
const App = component$(() => {
return (
<UserProfile
name="John"
age={20}
favouriteColors={["green", "blue", "red"]}
isAvailable
/>
);
});
export default App;
Emit to parent
Qwik
import { $, component$, useStore } from "@builder.io/qwik";
import AnswerButton from "./AnswerButton";
const App = component$(() => {
const store = useStore({
isHappy: true,
});
const onAnswerNo = $(() => {
store.isHappy = false;
});
const onAnswerYes = $(() => {
store.isHappy = true;
});
return (
<>
<p>Are you happy?</p>
<AnswerButton onYes$={onAnswerYes} onNo$={onAnswerNo} />
<p style={{ fontSize: 50 }}>{store.isHappy ? "😀" : "😥"}</p>
</>
);
});
export default App;
import { track } from "ripple";
import { AnswerButton } from "./AnswerButton.tsrx";
export function App() {
let &[isHappy] = track(true);
function onAnswerNo() {
isHappy = false;
}
function onAnswerYes() {
isHappy = true;
}
return <>
<p>"Are you happy?"</p>
<AnswerButton onYes={onAnswerYes} onNo={onAnswerNo} />
<p style={{ fontSize: "50px" }}>{isHappy ? "😀" : "😥"}</p>
</>;
}
Slot
Slot fallback
Qwik
import FunnyButton from "./FunnyButton";
export default function App() {
return (
<>
<FunnyButton />
<FunnyButton>Click me!</FunnyButton>
</>
);
}
Context
Qwik
import {
component$,
useStore,
useContextProvider,
createContext,
$,
} from "@builder.io/qwik";
import UserProfile from "./UserProfile";
export const UserContext = createContext("user-context");
const App = component$(() => {
const user = useStore({
id: 1,
username: "unicorn42",
email: "[email protected]",
});
const updateUsername = $((newUsername) => {
user.username = newUsername;
});
useContextProvider(UserContext, { user, updateUsername });
return (
<>
<h1>Welcome back, {user.username}</h1>
<UserProfile />
</>
);
});
export default App;
import { RippleObject } from "ripple";
import { UserContext } from "./UserContext.tsrx";
import { UserProfile } from "./UserProfile.tsrx";
export function App() {
const user = new RippleObject({
id: 1,
username: "unicorn42",
email: "[email protected]",
});
UserContext.set(user);
return <>
<h1>"Welcome back, "{user.username}</h1>
<UserProfile />
</>;
}
Form input
Input text
Qwik
import { component$, useSignal } from "@builder.io/qwik";
const InputHello = component$(() => {
const text = useSignal("");
return (
<>
<p>{text.value}</p>
<input bind:value={text} />
</>
);
});
export default InputHello;
Checkbox
Qwik
import { component$, useSignal } from "@builder.io/qwik";
const IsAvailable = component$(() => {
const isAvailable = useSignal(false);
return (
<>
<input id="is-available" type="checkbox" bind:checked={isAvailable} />
<label for="is-available">Is available</label>
</>
);
});
export default IsAvailable;
Radio
Qwik
import { component$, useSignal } from "@builder.io/qwik";
const PickPill = component$(() => {
const pickedColor = useSignal("red");
return (
<>
<div>Picked: {pickedColor.value}</div>
<input
id="blue-pill"
type="radio"
bind:value={pickedColor}
checked={pickedColor.value === "blue"}
value="blue"
/>
<label for="blue-pill">Blue pill</label>
<input
id="red-pill"
type="radio"
checked={pickedColor.value === "red"}
bind:value={pickedColor}
value="red"
/>
<label for="red-pill">Red pill</label>
</>
);
});
export default PickPill;
import { bindGroup, track } from "ripple";
export default function PickPill() {
let &[picked, pickedTracked] = track("red");
return <>
<div>"Picked: "{picked}</div>
<input
id="blue-pill"
type="radio"
value="blue"
ref={bindGroup(pickedTracked)}
/>
<label for="blue-pill">"Blue pill"</label>
<input
id="red-pill"
type="radio"
value="red"
ref={bindGroup(pickedTracked)}
/>
<label for="red-pill">"Red pill"</label>
</>;
}
Select
Qwik
import { component$, useSignal } from "@builder.io/qwik";
export const colors = [
{ id: 1, text: "red" },
{ id: 2, text: "blue" },
{ id: 3, text: "green" },
{ id: 4, text: "gray", isDisabled: true },
];
const ColorSelect = component$(() => {
const selectedColorId = useSignal("2");
return (
<select bind:value={selectedColorId}>
{colors.map((color) => (
<option
key={color.id}
value={color.id}
disabled={color.isDisabled}
selected={`${color.id}` === selectedColorId.value}
>
{color.text}
</option>
))}
</select>
);
});
export default ColorSelect;
import { bindValue, track } from "ripple";
const colors = [
{ id: 1, text: "red" },
{ id: 2, text: "blue" },
{ id: 3, text: "green" },
{ id: 4, text: "gray", isDisabled: true },
];
export default function ColorSelect() {
let &[selectedColorId, selectedColorIdTracked] = track(2);
return <select ref={bindValue(selectedColorIdTracked)}>
for (const color of colors; key color.id) {
<option value={color.id} disabled={color.isDisabled}>
{color.text}
</option>
}
</select>;
}
Webapp features
Render app
Qwik
<!doctype html>
<html>
<body>
<div id="app"></div>
<script type="module" src="./main.tsx"></script>
</body>
</html>
Ripple
<!doctype html>
<html>
<body>
<div id="app"></div>
<script type="module" src="./main.js"></script>
</body>
</html>
Fetch data
Qwik
import { component$, useResource$, Resource } from "@builder.io/qwik";
type UsersResponse = {
results: {
picture: {
thumbnail: string;
};
name: {
first: string;
last: string;
};
}[];
};
export async function fetchUsers() {
return (await fetch("https://randomuser.me/api/?results=3")).json();
}
export const App = component$(() => {
const data = useResource$<UsersResponse>(fetchUsers);
return (
<Resource
value={data}
onPending={() => <p>Fetching users...</p>}
onRejected={() => <p>An error occurred while fetching users</p>}
onResolved={({ results: users }) => (
<ul>
{users.map((user) => (
<li>
<img src={user.picture.thumbnail} alt="user" />
<p>
{user.name.first} {user.name.last}
</p>
</li>
))}
</ul>
)}
/>
);
});
import { useFetchUsers } from "./useFetchUsers.tsrx";
export function App() {
const data = useFetchUsers();
return <>
if (data.isLoading) {
<p>"Fetching users..."</p>
} else if (data.error) {
<p>"An error occurred while fetching users"</p>
} else if (data.users) {
<ul>
for (const user of data.users; key user.login.uuid) {
<li>
<img src={user.picture.thumbnail} alt="user" />
<p>{user.name.first}" "{user.name.last}</p>
</li>
}
</ul>
}
</>;
}