Here's what you'll put together: the full Nadia's Bookshop page. A search box filters the catalogue, an Add button fills the cart, the header says Cart: 3 books · $60, and a small form lets buyers request a title that isn't sold yet.
You've practised every part on its own: components, props, signals, memos, Show, For and events. In this project they come together into one app that is mounted on the page through one door, src/index.jsx. It's like opening day at the shop. The shelves, the price labels, the till and the request book were each prepared on their own. Today they are arranged into one shop, and the front door is opened once. After that, only small parts change: the number on the till, one line in the request book.
Project structure
A Solid project is usually made with Vite: npm create vite@latest bookshop -- --template solid. Roughly what it holds:
bookshop/
├── index.html ← has <div id="root"></div>
├── package.json
├── vite.config.js
└── src/
├── index.jsx ← the way in: render(...)
├── App.jsx
└── components/
├── Header.jsx
└── BookCard.jsx
src/index.jsx(orindex.tsxin a TypeScript project) is the entry point file: the first file that runs.App.jsxholds the data and puts the other components together. Small components live in thecomponents/folder.
index.jsx: render and dispose
// src/index.jsx
import { render } from "solid-js/web"
import App from "./App"
const dispose = render(() => <App />, document.getElementById("root"))
renderis imported from"solid-js/web", not from"solid-js". It takes a function that returns<App />, and the element to mount it in.renderreturns a dispose function. Callingdispose()takes the whole app down: every effect stops, everyonCleanupruns. Tested: afterdispose(),#rootis empty.
The shop App: every concept in one place
// src/App.jsx
import { createSignal, createMemo, For, Show } from "solid-js"
const catalog = [
{ id: 1, title: "The Rainbow Troops", price: 18, stock: 4 },
{ id: 2, title: "This Earth of Mankind", price: 24, stock: 2 },
{ id: 3, title: "Beauty Is a Wound", price: 19, stock: 0 },
]
const formatPrice = (n) => "$" + n.toLocaleString("en-US")
function BookCard(props) {
return (
<li>
{props.book.title} · {formatPrice(props.book.price)}
<Show when={props.book.stock > 0} fallback={<span> · Sold out</span>}>
<button onClick={() => props.onAdd(props.book)}>Add</button>
</Show>
</li>
)
}
export default function App() {
console.log("App runs")
const [search, setSearch] = createSignal("")
const [cart, setCart] = createSignal([])
const results = createMemo(() => catalog.filter((b) => b.title.toLowerCase().includes(search().toLowerCase())))
const total = createMemo(() => cart().reduce((t, b) => t + b.price, 0))
const add = (book) => setCart((c) => [...c, { ...book, key: Date.now() + Math.random() }])
const remove = (key) => setCart((c) => c.filter((b) => b.key !== key))
return (
<div>
<p>Cart: {cart().length} books · {formatPrice(total())}</p>
<input placeholder="Search title" onInput={(e) => setSearch(e.target.value)} />
<ul>
<For each={results()}>{(b) => <BookCard book={b} onAdd={add} />}</For>
</ul>
<ul>
<For each={cart()}>
{(b) => <li>{b.title} <button onClick={() => remove(b.key)}>Remove</button></li>}
</For>
</ul>
</div>
)
}
| Part | Tool | From the lesson |
|---|---|---|
| the search box's text, the cart's contents | createSignal |
Signals |
| the search result, the total price | createMemo |
Memos |
| the book list, the cart rows | For |
List Rendering |
the Sold out label for stock 0 |
Show |
Conditional Rendering |
BookCard receiving book and onAdd |
props | Props |
- Add The Rainbow Troops twice and This Earth of Mankind once:
Cart: 3 books · $60. Remove one The Rainbow Troops:Cart: 2 books · $42. - Removing means building a new array without that item:
c.filter((b) => b.key !== key). The row really leaves the list. totalis derived fromcartwithcreateMemo, not kept in a separate signal, so it always matches after an add or a remove.
A form: requesting a title that isn't in stock
Buyers may request a title the shop doesn't sell yet. This is the pattern of both exercises:
const [requests, setRequests] = createSignal([])
const [requestTitle, setRequestTitle] = createSignal("")
const requestCount = createMemo(() => requests().length)
const addRequest = (e) => {
e.preventDefault()
if (!requestTitle().trim()) return
setRequests((t) => [...t, { id: Date.now(), title: requestTitle() }])
setRequestTitle("")
}
<form onSubmit={addRequest}>
<input value={requestTitle()} onInput={(e) => setRequestTitle(e.target.value)} />
<button type="submit">Request</button>
</form>
<p>{requestCount()} titles requested</p>
<ul>
<For each={requests()}>{(t) => <li>{t.title}</li>}</For>
</ul>
<form onSubmit={...}>catches a click on thetype="submit"button and the Enter key.e.preventDefault()stops the page from reloading.if (!requestTitle().trim()) return: an empty box, or one with only spaces, adds no row.value={requestTitle()}binds the box to the signal, sosetRequestTitle("")really empties the box after a request is saved.- Exercise 1 (Todo App) uses this pattern plus a checkbox:
setTodos((t) => t.map((todo) => (todo.id === id ? { ...todo, done: !todo.done } : todo))), and the count of unfinished ones throughcreateMemo(() => todos().filter((t) => !t.done).length). Exercise 2 (Shopping List) uses exactly this pattern, with<p>Total items: {total()}</p>. In both exercises, the saving button readsAdd, and every row has aRemovebutton.
App runs once
The shop's App above has console.log("App runs") in its body, and App runs is printed once, when the page opens. Tested in Solid 1.9: three Add clicks, one Remove, and typing in the search box don't run App again. What reruns is only the part that reads the changed signal: total and the cart text when the cart changes, results and the book list when search changes.
⚠️ What makes the little shop misbehave
- Destructuring props in the parameters.
function Header({ count, total })reads its values once. Tested: after three Add clicks, the header still saysCart: 0 books · $0. Useprops.count. - Forgetting
e.preventDefault(). The form submits and reloads the page, so the list is gone. - Hiding a row instead of removing it. The exercise checks that the
<li>really leaves the<ul>after Remove, and that your code containsfilter(. - Forgetting to empty the box or to ignore an empty entry. The exercise checks the box is empty after Add, and that Add with an empty box makes no
<li>.
TL;DR: A Solid project starts from src/index.jsx (or index.tsx): render(() => <App />, document.getElementById("root")) from "solid-js/web", which returns a dispose function that takes the app down. App puts everything together: createSignal for inputs and lists, createMemo for derived values such as a total or a count, For for lists, Show for conditions, props for small components. A form uses onSubmit + e.preventDefault(), ignores an empty entry, adds with [...old, item], empties its box, and removes with filter. App runs once; after that, only the parts that read a changed signal are updated.
⚡ One way in: render
Write:
// src/index.jsx
import { render } from "solid-js/web"
import App from "./App"
const dispose = render(() => <App />, document.getElementById("root"))
Meaning: src/index.jsx is the first file that runs. render from "solid-js/web" takes a function that returns <App />, and returns a dispose function that takes the whole app down.
⚡ A form: add, empty, ignore what's empty
Write:
const addRequest = (e) => {
e.preventDefault()
if (!requestTitle().trim()) return
setRequests((t) => [...t, { id: Date.now(), title: requestTitle() }])
setRequestTitle("")
}
Meaning: e.preventDefault() stops the page from reloading, an empty box adds no row, and value={requestTitle()} makes setRequestTitle("") really empty the box. This exercise checks the box is empty again after Add.
⚡ Removing means a new array
Write:
const remove = (key) => setCart((c) => c.filter((b) => b.key !== key))
Meaning: Removing means building a new array without that item, so the row really leaves the list. total is derived from the cart with createMemo, so it always matches after an add or a remove.
⚡ App still runs once
Guess first: App has console.log("App runs"). Three Add clicks, one Remove, then typing in the search box. How many times does App runs print?
Once, when the page opens. What reruns is only the part that reads the changed signal.