Getting Started
The core of a single-page application is its router. The router is responsible for matching locations to its known routes and for powering navigation within the application.
The Router
A router is created using a history
function and a routes
array.
URLs
Locations are represented using URLs. URLs are a combination of a pathname
string, a query
, and a hash
.
The router provides a url
method for automatically generating a URL. The method takes the name of the route and the route's params to generate the URL's pathname
. Additionally, query
and hash
values can be provided.
By default, a query
is a string, but you can also configure your history to use a query library.
Navigation
There are two types of navigation within a single-page application: in-app navigation (e.g. clicking a link) and platform navigation (e.g. clicking the back button or typing a URL in the address bar and hitting enter).
A Curi router object has a navigate
method to let you navigate with code. The function takes an object with a url
property of the URL to navigate to; this pairs well with the router's url
method described above. There are also a number of other optional arguments to navigate
.
Response Handlers
When Curi matches a location to a route, it creates a "response" object. Respons objects provide information about the route that matched.
Response handlers are functions that will be called when there is a new response. There are three types of response handlers: side effects, one time functions, and observers.
Side effects are passed to the router when you are creating it. These are best suited for non-rendering tasks. You can read more about them in the side effects guide.
Response handlers registered with router.once
will only be called one time. This is primarily useful for waiting for asynchronous actions to finish before the initial render.
Observers are passed to the router using router.observe
. Unlike one time functions, these will be called every time there is a new response.
Render packages, like @curi/react-dom
, use router.observe
internally in order to re-render when there is a new response.
Rendering
Curi adapts its API to work with different UI libraries. You can check out the respective guides for the officially supported libraries to see how to use them.