Solid Project — SolidJS

A Solid project starts from src/index.jsx (or index.tsx): render(() => <App />, document.getElementById("root")) from "solid-js/web", which returns a dispose…

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

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"))

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

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>

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

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.

What you will learn