Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ title: Qwik Tutorial - Listeners
layout: tutorial
---

In this tutorial, we will add an event listener to the input. We will use the `keyup` event to listen to changes on the input. The goal is to update the `organization` as the user types.
In this tutorial, we will add an event listener to the input. We will use the `keyup` event to listen to changes on the input. The goal is to update the `org` as the user types.

Use the `onKeyUp$` property on the `<input>` element to add an event listener.

Notice that the assignment to the store (`github.organization = ...`) automatically triggers the component re-rendering.
Notice that the assignment to the store (`github.org = ...`) automatically triggers the component re-rendering.
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
/* eslint-disable no-console */
import {
component$,
useStore,
useWatch$,
getPlatform,
useHostElement,
useServerMount$,
} from '@builder.io/qwik';
import { component$, useStore, useWatch$, useServerMount$ } from '@builder.io/qwik';
import { isServer } from '@builder.io/qwik/build';

export const App = component$(() => {
const github = useStore({
Expand All @@ -19,7 +13,7 @@ export const App = component$(() => {
useWatch$((track) => {
track(github, 'org');

if (getPlatform(useHostElement()).isServer) return;
if (isServer) return;

github.repos = null;
const controller = new AbortController();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
/* eslint-disable no-console */
import {
component$,
useStore,
useWatch$,
getPlatform,
useHostElement,
useServerMount$,
} from '@builder.io/qwik';
import { component$, useServerMount$, useStore, useWatch$ } from '@builder.io/qwik';
import { isServer } from '@builder.io/qwik/build';

export const App = component$(() => {
const github = useStore({
Expand All @@ -21,7 +15,7 @@ export const App = component$(() => {
useWatch$((track) => {
track(github, 'org');

if (getPlatform(useHostElement()).isServer) return;
if (isServer) return;

github.repos = null;
const controller = new AbortController();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ layout: tutorial

For this tutorial, we would like to fetch the list of repositories for a given GitHub organization. To aid you, we have added the `getRepositories()` function to the bottom of the file. Your task is to use the `getRepositories()` function to fetch the list of repositories whenever the user updates the `organization` input.

You will need to use `useWatch()` to observe changes to the `github.organization`, and on each update of the organization, invoke `getRepositories()`. Here is an example of how to use `useWatch()`
You will need to use `useWatch()` to observe changes to the `github.org`, and on each update of the organization, invoke `getRepositories()`. Here is an example of how to use `useWatch$()`

```typescript
useWatch$((track) => {
// STEP 1: Tell Qwik which propreties it should track and re-run this function whenever they change.
track(github, "organization");
track(github, "org");

// STEP 2: For now, we don't want to run this code on the server. We will cover SSR in the later part.
if (getPlatform(useHostElement()).isServer) return;
if (isServer) return;

// STEP 3: Perform the fetching of the repositories.
github.repos = null;
const controller = new AbortController();
getRepositories(github.organization, controller).then(
getRepositories(github.org, controller).then(
(repos) => (github.repos = repos)
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { component$, useStore, useWatch$, getPlatform, useHostElement } from '@builder.io/qwik';
import { component$, useStore, useWatch$ } from '@builder.io/qwik';
import { isServer } from '@builder.io/qwik/build';

export const App = component$(() => {
const github = useStore({
Expand All @@ -9,7 +10,7 @@ export const App = component$(() => {
useWatch$((track) => {
track(github, 'org');

if (getPlatform(useHostElement()).isServer) return;
if (isServer) return;

github.repos = null;
const controller = new AbortController();
Expand Down