brumate

Your first app

This tutorial builds a todo list in six steps: a list, a form that adds items, toggling items, a count of what is left, saving on the server, and a route. Each step shows the whole component, and every sample before the last runs in the page, so you can change it and see what happens.

1. Show a list

A signal holds the todos, and #for renders one item for each:

App.brmEdit the code to run it again

The sample runs when it comes into view.

2. Add items

A second signal holds the text of the field. Submitting the form adds a todo with it:

App.brmEdit the code to run it again

The sample runs when it comes into view.

3. Toggle items

A checkbox on each item marks it done. $produce changes one item in place on a copy, and writes the copy:

App.brmEdit the code to run it again

The sample runs when it comes into view.

4. Count what is left

A memo computes a value from other values. It updates when they change:

App.brmEdit the code to run it again

The sample runs when it comes into view.

5. Save on the server

A function with 'use server' runs on the server. In the browser, calling it sends a request. Here the server keeps the todos in a list of its module. This sample's server runs in the page, so the list resets when you edit the code:

App.brmEdit the code to run it again

The sample runs when it comes into view.

Server functions describes the rules, errors and the request.

6. Move it into a route

In an app, the todo list is a component that a route renders. Move the component to src/Todos.brm, and render it from a route file:

<!-- src/routes/todos.brm -->
<script>
  import Todos from '../Todos.brm';
</script>

<Todos />

The page is now at /todos, inside the layout of src/routes/_layout.brm. Add a link to it in the layout:

<nav>
  <a href="/">Home</a>
  <a href="/todos">Todos</a>
</nav>
<main>{children}</main>

Once the loader runs, a click on a link loads the next page without a full page load, and the layout stays. Routing and Navigation describe both.

Next