Getting Started Qwikly

Qwik is a new kind of framework that is resumable (no eager JS execution and no hydration), built for the edge and familiar to React developers.

To play with it right away, check out Qwikโ€™s in-browser playgrounds:

Prerequisites

To get started with Qwik locally, you need the following:

Create an app using the CLI

First, use the Qwik CLI to generate a blank starter application, to quickly familiarize yourself with it. The same command can be used to create projects for either Qwik or Qwik city.

Run the Qwik CLI in your shell. Qwik supports pnpm, npm, yarn and bun. Choose the package manager you prefer and run one of the following commands:

pnpm create qwik@latest

The CLI guides you through an interactive menu to set the project-name, select one of the starters, and ask if you want to install the dependencies. To find out more about the files generated, refer to the Project Structure documentation.

Start the development server:

pnpm run start

Qwik Joke App

The Qwik Hello World tutorial guides you through building a joke app with Qwik while covering the most important Qwik concepts. The app displays a random joke from https://icanhazdadjoke.com and features a button to get a new joke on click.

1. Create A Route

Start by serving a page at a particular route. This basic app serves a random dad joke application on the /joke/ route. This tutorial relies on Qwikcity, Qwik's meta-framework, which uses directory-based routing. To get started:

  1. In your project, create a new joke directory in routes containing an index.tsx file.
  2. Each route's index.tsx file must have an export default component$(...) so that Qwikcity knows what content to serve. Paste the following content to src/routes/joke/index.tsx:
src/routes/joke/index.tsx
import { component$ } from '@qwik.dev/core';
 
export default component$(() => {
  return <section class="section bright">A Joke!</section>;
});
  1. Navigate to localhost:5173/joke/ to see your new page working.

NOTE:

  • Your joke route default component is surrounded by an existing layout. See Layout for more details on what layouts are and how to work with them.
  • index.tsx, layout.tsx in routes folder, root.tsx and all entry files need export default. For other components you can use export const and export function
  • For more details about how to author components, see the Component API section.

2. Loading Data

Use the external JSON API at https://icanhazdadjoke.com to load random jokes. You can use route loaders to load this data into the server and render it in the component.

  1. Open src/routes/joke/index.tsx and add this code:
src/routes/joke/index.tsx
import { component$ } from '@qwik.dev/core';
import { routeLoader$ } from '@qwik.dev/city';
 
export const useDadJoke = routeLoader$(async () => {
  const response = await fetch('https://icanhazdadjoke.com/', {
    headers: { Accept: 'application/json' },
  });
  return (await response.json()) as {
    id: string;
    status: number;
    joke: string;
  };
});
 
export default component$(() => {
  // Calling our `useDadJoke` hook, will return a reactive signal to the loaded data.
  const dadJokeSignal = useDadJoke();
  return (
    <section class="section bright">
      <p>{dadJokeSignal.value.joke}</p>
    </section>
  );
});
  1. Now on http://localhost:5173/joke/, the browser displays a random joke.

Code explanation:

  • The function passed to routeLoader$ is invoked on the server eagerly before any component is rendered and is responsible for loading data.
  • The routeLoader$ returns a use-hook, useDadJoke(), that can be used in the component to retrieve the server data.

NOTE

  • The routeLoader$ is invoked eagerly on the server before any component is rendered, even if its use-hook is not invoked in any component.
  • The routeLoader$ return type is inferred in the component without the need for any additional type information.

3. Posting Data to the Server

Previously, the component routeLoader$ was used to send data from the server to the client. To post (send) data from the client back to the server, use routeAction$.

NOTE: routeAction$ is the preferred way to send data to the server because it uses the browser native form API, which works even if JavaScript is disabled.

To declare an action, add this code:

src/routes/joke/index.tsx
import { routeLoader$, Form, routeAction$ } from '@qwik.dev/city';
 
export const useJokeVoteAction = routeAction$((props) => {
  // Leave it as an exercise for the reader to implement this.
  console.log('VOTE', props);
});
  1. Update the export default component to use the useJokeVoteAction hook with <Form>.
src/routes/joke/index.tsx
export default component$(() => {
  const dadJokeSignal = useDadJoke();
  const favoriteJokeAction = useJokeVoteAction();
  return (
    <section class="section bright">
      <p>{dadJokeSignal.value.joke}</p>
      <Form action={favoriteJokeAction}>
        <input type="hidden" name="jokeID" value={dadJokeSignal.value.id} />
        <button name="vote" value="up">๐Ÿ‘</button>
        <button name="vote" value="down">๐Ÿ‘Ž</button>
      </Form>
    </section>
  );
});
  1. Now the buttons display on http://localhost:5173/joke/, and their values will log to the console when they are clicked.

Code explanation:

  • routeAction$ receives the data.
    • The function passed to routeAction$ is invoked on the server whenever the form is posted.
    • The routeAction$ returns a use-hook, useJokeVoteAction, that you can use in the component to post the form data.
  • Form is a convenience component that wraps the browser's native <form> element.

Things to note:

  • For validation, see zod validation.
  • The routeAction$ works even if JavaScript is disabled.
  • If JavaScript is enabled, the Form component will prevent the browser from posting the form and instead post the data using JavaScript and emulate the browser's native form behavior without a full refresh.

For reference, the complete code snippet for this section is as follows:

src/routes/joke/index.tsx
import { component$ } from '@qwik.dev/core';
import { routeLoader$, Form, routeAction$ } from '@qwik.dev/city';
 
export const useDadJoke = routeLoader$(async () => {
  const response = await fetch('https://icanhazdadjoke.com/', {
    headers: { Accept: 'application/json' },
  });
  return (await response.json()) as {
    id: string;
    status: number;
    joke: string;
  };
});
 
export const useJokeVoteAction = routeAction$((props) => {
  console.log('VOTE', props);
});
 
export default component$(() => {
  // Calling our `useDadJoke` hook, will return a reactive signal to the loaded data.
  const dadJokeSignal = useDadJoke();
  const favoriteJokeAction = useJokeVoteAction();
  return (
    <section class="section bright">
      <p>{dadJokeSignal.value.joke}</p>
      <Form action={favoriteJokeAction}>
        <input type="hidden" name="jokeID" value={dadJokeSignal.value.id} />
        <button name="vote" value="up">
          ๐Ÿ‘
        </button>
        <button name="vote" value="down">
          ๐Ÿ‘Ž
        </button>
      </Form>
    </section>
  );
});

4. Modifying State

Keeping track of the state and updating the UI is core to what applications do. Qwik provides a useSignal hook to keep track of the application's state. To learn more, see state management.

To declare state:

  1. Import useSignal from qwik.
    import { component$, useSignal } from "@qwik.dev/core";
  2. Declare the component's state using useSignal().
    const isFavoriteSignal = useSignal(false);
  3. After the closing Form tag, add a button to the component to modify the state.
    <button
     onClick$={() => {
       isFavoriteSignal.value = !isFavoriteSignal.value;
     }}>
      {isFavoriteSignal.value ? 'โค๏ธ' : '๐Ÿค'}
    </button>

NOTE: Clicking on the button updates the state, which in turn updates the UI.

For reference, the complete code snippet for this section is as follows:

src/routes/joke/index.tsx
import { component$, useSignal } from '@qwik.dev/core';
import { routeLoader$, Form, routeAction$ } from '@qwik.dev/city';
 
export const useDadJoke = routeLoader$(async () => {
  const response = await fetch('https://icanhazdadjoke.com/', {
    headers: { Accept: 'application/json' },
  });
  return (await response.json()) as {
    id: string;
    status: number;
    joke: string;
  };
});
 
export const useJokeVoteAction = routeAction$((props) => {
  console.log('VOTE', props);
});
 
export default component$(() => {
  const isFavoriteSignal = useSignal(false);
  // Calling our `useDadJoke` hook, will return a reactive signal to the loaded data.
  const dadJokeSignal = useDadJoke();
  const favoriteJokeAction = useJokeVoteAction();
 
  return (
    <section class="section bright">
      <p>{dadJokeSignal.value.joke}</p>
      <Form action={favoriteJokeAction}>
        <input type="hidden" name="jokeID" value={dadJokeSignal.value.id} />
        <button name="vote" value="up">
          ๐Ÿ‘
        </button>
        <button name="vote" value="down">
          ๐Ÿ‘Ž
        </button>
      </Form>
      <button
        onClick$={() => (isFavoriteSignal.value = !isFavoriteSignal.value)}
      >
        {isFavoriteSignal.value ? 'โค๏ธ' : '๐Ÿค'}
      </button>
    </section>
  );
});

5. Tasks and Invoking Server Code

In Qwik, a task is work that needs to happen when a state changes. (This is similar to an "effect" in other frameworks.) In this example, we use the task to invoke code on the server.

  1. Import useTask$ from qwik and $server from qwik-city.

    import { component$, useSignal, useTask$ } from "@qwik.dev/core";
    import {
      routeLoader$,
      Form,
      routeAction$,
      server$,
    } from '@qwik.dev/city';
  2. Create a new task that tracks the isFavoriteSignal state:

    useTask$(({ track }) => {});
  3. Add a track call to re-execute the task on isFavoriteSignal state change:

    useTask$(({ track }) => {
      track(() => isFavoriteSignal.value);
    });
  4. Add the work that you want to execute on state change:

    useTask$(({ track }) => {
      track(() => isFavoriteSignal.value);
      console.log('FAVORITE (isomorphic)', isFavoriteSignal.value);
    });
  5. If you want to have work happen on the server only, wrap it in server$()

    useTask$(({ track }) => {
      track(() => isFavoriteSignal.value);
      console.log('FAVORITE (isomorphic)', isFavoriteSignal.value);
      server$(() => {
        console.log('FAVORITE (server)', isFavoriteSignal.value);
      })();
    });

NOTE:

  • The body of useTask$ is executed on both the server and the client (isomorphic).
  • On SSR, the server prints FAVORITE (isomorphic) false and FAVORITE (server) false.
  • When the user interacts with favorite, the client prints FAVORITE (isomorphic) true and the server prints FAVORITE (server) true.

For reference, the complete code snippet for this section is as follows:

src/routes/joke/index.tsx
import { component$, useSignal, useTask$ } from '@qwik.dev/core';
import {
  routeLoader$,
  Form,
  routeAction$,
  server$,
} from '@qwik.dev/city';
 
export const useDadJoke = routeLoader$(async () => {
  const response = await fetch('https://icanhazdadjoke.com/', {
    headers: { Accept: 'application/json' },
  });
  return (await response.json()) as {
    id: string;
    status: number;
    joke: string;
  };
});
 
export const useJokeVoteAction = routeAction$((props) => {
  console.log('VOTE', props);
});
 
export default component$(() => {
  const isFavoriteSignal = useSignal(false);
  // Calling our `useDadJoke` hook, will return a reactive signal to the loaded data.
  const dadJokeSignal = useDadJoke();
  const favoriteJokeAction = useJokeVoteAction();
  useTask$(({ track }) => {
    track(() => isFavoriteSignal.value);
    console.log('FAVORITE (isomorphic)', isFavoriteSignal.value);
    server$(() => {
      console.log('FAVORITE (server)', isFavoriteSignal.value);
    })();
  });
  return (
    <section class="section bright">
      <p>{dadJokeSignal.value.joke}</p>
      <Form action={favoriteJokeAction}>
        <input type="hidden" name="jokeID" value={dadJokeSignal.value.id} />
        <button name="vote" value="up">
          ๐Ÿ‘
        </button>
        <button name="vote" value="down">
          ๐Ÿ‘Ž
        </button>
      </Form>
      <button
        onClick$={() => (isFavoriteSignal.value = !isFavoriteSignal.value)}
      >
        {isFavoriteSignal.value ? 'โค๏ธ' : '๐Ÿค'}
      </button>
    </section>
  );
});

6. Styling

Styling is an important part of any application. Qwik provides a way to associate and scope styles with your component.

To add styles:

  1. Create a new file src/routes/joke/index.css:

    p {
      font-weight: bold;
    }
     
    form {
      float: right;
    }
  2. import the styles in src/routes/joke/index.tsx:

    import styles from "./index.css?inline";
  3. Import useStylesScoped$ from qwik.

    import { component$, useSignal, useStylesScoped$, useTask$ } from "@qwik.dev/core";
  4. Tell the component to load the styles:

    useStylesScoped$(styles);

Code explanation:

  • The ?inline query parameter tells Vite to inline the styles into the component.
  • The useStylesScoped$ call tells Qwik to associate the styles with the component only (scoping).
  • Styles are only loaded if they are not already inlined as part of SSR and only for the first component.

For reference, the complete code snippet for this section is as follows:

src/routes/joke/index.tsx
import {
  component$,
  useSignal,
  useStylesScoped$,
  useTask$,
} from '@qwik.dev/core';
import {
  routeLoader$,
  Form,
  routeAction$,
  server$,
} from '@qwik.dev/city';
import styles from './index.css?inline';
 
export const useDadJoke = routeLoader$(async () => {
  const response = await fetch('https://icanhazdadjoke.com/', {
    headers: { Accept: 'application/json' },
  });
  return (await response.json()) as {
    id: string;
    status: number;
    joke: string;
  };
});
 
export const useJokeVoteAction = routeAction$((props) => {
  console.log('VOTE', props);
});
 
export default component$(() => {
  useStylesScoped$(styles);
  const isFavoriteSignal = useSignal(false);
  // Calling our `useDadJoke` hook, will return a reactive signal to the loaded data.
  const dadJokeSignal = useDadJoke();
  const favoriteJokeAction = useJokeVoteAction();
  useTask$(({ track }) => {
    track(() => isFavoriteSignal.value);
    console.log('FAVORITE (isomorphic)', isFavoriteSignal.value);
    server$(() => {
      console.log('FAVORITE (server)', isFavoriteSignal.value);
    })();
  });
  return (
    <section class="section bright">
      <p>{dadJokeSignal.value.joke}</p>
      <Form action={favoriteJokeAction}>
        <input type="hidden" name="jokeID" value={dadJokeSignal.value.id} />
        <button name="vote" value="up">๐Ÿ‘</button>
        <button name="vote" value="down">๐Ÿ‘Ž</button>
      </Form>
      <button
        onClick$={() => (isFavoriteSignal.value = !isFavoriteSignal.value)}
      >
        {isFavoriteSignal.value ? 'โค๏ธ' : '๐Ÿค'}
      </button>
    </section>
  );
});

7. Preview

Up until now, you've been using the dev server to make your application.

This is great to see your changes in real time, but it doesn't work the same way as in production:

  • Each file is loaded individually, which may cause waterfalls in the network tab.
  • There is no speculative loading of bundles, so there may be a delay on the first interaction.

To make sure everything is ready for production and eliminate these issues, you can run your app in preview:

  1. Run preview command to create a production build and run it.
pnpm run preview

NOTE:

  • Your application should now have a production build running on localhost:4173.
  • If you interact with the application now, the network tab of the dev tools should show that the bundles are instantly delivered from the ServiceWorker cache.

Review

Congratulations, you've learned a lot about Qwik! For more on just how much you can achieve with Qwik, check out the dedicated docs on each of the topics touched on in this tutorial:

Contributors

Thanks to all the contributors who have helped make this documentation better!

  • manucorporat
  • jesperp
  • adamdbradley
  • steve8708
  • cunzaizhuyi
  • mousaAM
  • zanettin
  • Craiqser
  • MyltsinVV
  • literalpie
  • colynyu
  • the-r3aper7
  • ahmadalfy
  • renomureza
  • mhevery
  • AnthonyPAlicea
  • kapunahelewong
  • kushalmahajan
  • sreeisalso
  • dustinsgoodman
  • nsdonato
  • seqshem
  • ryo-manba
  • EamonHeffernan
  • DKozachenko
  • mrhoodz
  • moinulmoin
  • lanc3lo1
  • johnrackles
  • kushalvmahajan
  • daniela-bonvini
  • jemsco
  • maiieul