Button with a typed prop contract, then a Counter that adds reactive state with useState. Along the way you’ll see how Solar validates props at the boundary, what a ContractError looks like when you pass the wrong type, and how to inspect your component registry. By the end, you’ll have a working Solar project and a clear mental model of how the framework’s core pieces fit together.
Create your project
Scaffold a new Solar project with the CLI, then start the dev server:Open
http://localhost:3000. You’ll see the starter App component running. Now open components/App.js — that’s where you’ll work for the rest of this guide.Define your first component
Replace the contents of The
components/Button.js (create it if it doesn’t exist) with the following. This is a complete Solar component: it declares a prop schema, renders to the DOM using createElement, registers itself, and exports its definition.props object is your component’s contract. Every key declares a type, and optionally required: true, an enum of allowed values, or a default. Solar validates every prop at call time — before render ever runs.Mount it to the DOM
Open Save the file and check your browser. The button renders and logs to the console when clicked.
main.js and mount your Button to the page. mountComponent takes the component function, a props object, and a DOM container, and returns a numeric id you can later pass to unmountComponent to remove the component.Add state with useState
Create Mount it in
components/Counter.js. This component uses useState to track a count value and re-renders whenever it changes.main.js alongside the button:useState returns the current value and a setter function. Pass the setter a new value, or a function that receives the previous value and returns the next one — both forms work.See contract validation in action
Pass the wrong type for a required prop and Solar throws a The error’s Every field is machine-readable. An AI agent can inspect
ContractError immediately — before any rendering happens..toJSON() method returns a structured object:prop, expected, and fix directly and regenerate the correct call without parsing a freeform error string.Next steps
Now that you have a working component with contract validation and state, explore the rest of Solar’s building blocks.Components
Learn how
defineComponent, slots, and typed composition work in depth.Contracts
Understand the full prop schema syntax, enum validation, slot types, and
ContractError structure.