Skip to main content

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.

Try it online

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:

  1. Import the package and the styles - import { Booking } from "@dhx/trial-booking" and its booking.css.
  2. Create a container ref with useRef() and render it as a <div> with a height.
  3. Initialize in useEffect - call new Booking(container.current, { data }) with an empty dependency array so it runs once.
  4. Pass the cards from the parent as a prop, and hand them to the configuration as data.
  5. Answer the confirmation in that same effect - booking.setConfirmHandler(event => ...) runs when a visitor confirms a slot. The demo waits a second and picks confirm.done() or confirm.error() at random, so both results of the booking dialog are visible.
  6. Clean up in the returned function by calling booking.destructor() so nothing is left behind on unmount.

API reference​

Additional resources​