Build a local-first To-Do app
A full step-by-step guide to building a simple to-do app using Basic’s create-lofi-app
In this guide, you will:
- Create a new local-first project using create-lofi-app
- Create a simple To-Do UI
- Connect to a Basic project and set up your schema
- Add sign in and sign out functionality
- Hook up to Basic DB so that your users can save their to-dos across their devices
Create a new local-first project
Create a new local-first project
When prompted in the CLI, enter y
for installing the vite package.
Then give your project a name.
And finally choose the Basic + Tailwind variant.
install npm packages
cd project-name
into your project, run npm i
, and start your server with npm run dev
:
You’ll now be able to navigate to http://localhost:5173
and see the app running as below:
Create a simple To-Do UI
We’ll replace the existing homepage UI with a simple To-Do app UI using Tailwind that comes preinstalled with the create-lofi-app template.
Delete the existing homepagecode
Let’s replace the code in your src/App.tsx
file with the following for a clean slate:
Add an input + button
Let’s add an input, and a button to the app, and add some basic Tailwind styling
You should now see a simple input and button on the page:
Connect to a Basic project and set up your schema
The fastest way to connect to a Basic project is to use the Basic CLI.
Install the Basic CLI
Make sure you are in the directory of your project.
You’ll be redirected in your browser to sign up for a Basic account and login. Once you’ve logged in, you’ll be able to connect to a Basic project.
Connect to a Basic project
Choose to Create new project
You’ll then be prompted to give your Basic project a name (this doesn’t have to match your directory name, but it’s best to keep it consistent), and to select a project template.
Choose the typescript
and hit Yep!
Set up your schema
If you navigate to the src/basic.config.ts
file, you’ll see that it’s already set up for you now with a schema object with a project_id
!
Let’s change the schema to match the needs of our To-Do app, and add a name string field and a completed boolean field.
Push the changes to our schema
Add sign in and sign out functionality
It’s very easy to add the ability to have your users sign in and sign out of your to-do app.
Add a login and logout button to App.tsx
Now navigate to src/App.tsx
and import signin
, signout
, isSignedIn
, and user
.
All of it put together should look like this when signed in:
Hook up to Basic DB
Now that we have the ability to sign in and sign out, we can hook up to Basic DB so that our users can save their to-dos across their devices.
Submit a task to Basic DB
We will add some state to capture our input value, implement the .add()
method, and then console.log
the result.
Read and display the tasks
Now let’s retrieve and display the tasks!
With just a few lines you were able to display your tasks as such:
Add a delete button
Now let’s add a delete button to our tasks using the .delete()
method.
The delete button would look like this:
And that’s it! You’ve now built a local-first to-do app that allows your users to sign in, sign out, and save their to-dos across their devices.
You’ll also notice how snappily all the changes are reflected on the app, from task creation to deletion - this is the power of local-first architecture!