What's an example section without a todo list?! Let's put one together in Raycast. This example will show how to render a list, navigate to a form to create a new element and update the list.
Render todo list
Let's start with a set of todos and simply render them as a list in Raycast:
import { List } from"@raycast/api";import { useState } from"react";interfaceTodo { title:string; isCompleted:boolean;}exportdefaultfunctionCommand() {const [todos,setTodos] =useState<Todo[]>([ { title:"Write a todo list extension", isCompleted:false }, { title:"Explain it to others", isCompleted:false }, ]);return ( <List> {todos.map((todo, index) => (<List.Item key={index} title={todo.title} /> ))}</List> );}
For this we define a TypeScript interface to describe out Todo with a title and a isCompleted flag that we use later to complete the todo. We use React's useState hook to create a local state of our todos. This allows us to update them later and the list will get re-rendered. Lastly we render a list of all todos.
Create a todo
A static list of todos isn't that much fun. Let's create new ones with a form. For this, we create a new React component that renders the form:
The <CreateTodoForm> shows a single text field for the title. When the form is submitted, it calls the onCreate callback and closes itself.
To use the action, we add it to the <List> component. This makes the action available when the list is empty which is exactly what we want to create our first todo.
Now that we can create new todos, we also want to make sure that we can tick off something on our todo list. For this, we create a <ToggleTodoAction> that we assign to the <List.Item>:
In this case we added the <ToggleTodoAction> to the list item. By doing this we can use the index to toggle the appropriate todo. We also added an icon to our todo that reflects the isCompleted state.
Delete a todo
Similar to toggling a todo, we also add the possibility to delete one. You can follow the same steps and create a new <DeleteTodoAction> and add it to the <List.Item>.
We also gave the <DeleteTodoAction> a keyboard shortcut. This way users can delete todos quicker. Additionally, we also added the <CreateTodoAction> to the <List.Item>. This makes sure that users can also create new todos when there are some already.
And that's a wrap. You created a todo list in Raycast, it's that easy. As next steps, you could extract the <CreateTodoForm> into a separate command. Then you can create todos also from the root search of Raycast and can even assign a global hotkey to open the form. Also, the todos aren't persisted. If you close the command and reopen it, they are gone. To persist, you can use the storage or write it to disc.