DHTMLX Booking. React integration example
Demo of DHTMLX Booking integration with React using the official GitHub demo. Shows initialization, data as props, a confirm handler, and cleanup for a React component. For a step-by-step setup, see the detailed React integration guide.
View on GitHub - open the repository and click "Open in Codespaces" on the README page to launch a configured React app with DHTMLX Booking. No local setup required.
Booking mounts into a plain DOM element, so React needs only the two standard hooks: useRef for the container and useEffect for the lifecycle. The widget is wrapped in one component that behaves like any other component in the app.
The effect calls new Booking(container.current, { data }) after React renders the element, so the node exists before the widget looks for it. The cards arrive from the parent as a data prop and go straight into the configuration. In the same effect the demo calls setConfirmHandler, which answers each slot a visitor confirms. The cleanup function returned from the effect calls destructor(), which releases the widget when the component unmounts.
Solution overview
To run DHTMLX Booking inside a React application:
- Import the package and the styles -
import { Booking } from "@dhx/trial-booking"and itsbooking.css. - Create a container ref with
useRef()and render it as a<div>with a height. - Initialize in
useEffect- callnew Booking(container.current, { data })with an empty dependency array so it runs once. - Pass the cards from the parent as a prop, and hand them to the configuration as
data. - Answer the confirmation in that same effect -
booking.setConfirmHandler(event => ...)runs when a visitor confirms a slot. The demo waits a second and picksconfirm.done()orconfirm.error()at random, so both results of the booking dialog are visible. - Clean up in the returned function by calling
booking.destructor()so nothing is left behind on unmount.
API reference
- Integration with React: Project setup, component, and cleanup
- data: The card and slot format the prop carries
- setConfirmHandler(): Handling a confirmed slot and reporting the result
- Properties overview: Every configuration property
Related examples
- Angular integration: the widget inside an Angular component
- Vue integration: the widget inside a Vue component
- Initialization: the base setup with cards, slots, and a confirm handler