React
API#
The React headless API consts of one global function init and two hooks, useSolid and useHistory. Init connects to ClassCAD. Solid and History open sessions and have different APIs. Solid is for creating geometry, History is for creating history-based geometry, and manipulating it later on with constraints and paremeters
Both hooks, useSolid and useHistory, return the same API:
Usage#
You must call init before you can use the headless API, this connects your browser tab to ClassCAD. If you provide a "ws://..." URL, the connection will be made via WebSocket. If you provide a "http://..." URL, the connection will be via WASM (the endpoint must provide a valid classcad.wasm).
You should do this in the root of your application.
Then you can use the headless API in your components. useSolid or useHistory will provide the necessary methods and data, as well as a Geometry component that is primed to the session, you can use it to render out the objects that you have created.
By default, the headless instance is named default, if you don't provide a name every call to useSolid or useHistory will return the same instance, no matter in which component you call it.
You can give the headless instance a custom name, which allows you to refer to it in multiple components:
Or, create multiple headless instances, but whose state you can still access between components:
Run#
run Executes ClassCAD commands. You can call it in events, effects, or anywhere you like.
Cache#
cache suspends and returns the result of the function as a cached and memoized value. The results can be displayed in whichever way you like, for instance returning a geometry and adding it to a mesh right away.
The dependecies are cache keys. Similar to a useMemo, the inner function is called when the cache keys change. The dependencies are also passed to the inner function, following api so you could hoist it.