ProSchedulerSign in

Scheduler · Loading events

Load events on demand

onPageChange reports the visible range on mount and on every page change; load just that range into data.

Live demo

Page forward and watch the status line · Switch the span to Month

Fullscreen
import { useState } from "react"
import { ProScheduler } from "@andreisaac/pro-scheduler"
import { studioResources, studioSessions, weekStart, type StudioEvent } from "./data/studio"
import { useEvents } from "./kit"

export function LoadOnDemandDemo() {
  const { events, setEvents, handlers } = useEvents<StudioEvent>([])
  const [status, setStatus] = useState("Loading…")

  return (
    <div className="docs-demo__stack">
      <p className="demo-status">{status}</p>
      <ProScheduler
        data={events}
        resources={studioResources}
        view={{ scheduler: { type: "week", startTime: "07:00", endTime: "20:00" } }}
        // Fires on mount and on every page change with the visible range.
        onPageChange={({ firstDay, lastDay }) => {
          setStatus("Loading…")
          const started = performance.now()
          // Stand-in for a request: build the weeks in [firstDay, lastDay).
          setTimeout(() => {
            const loaded: StudioEvent[] = []
            for (let w = weekStart(firstDay); w < lastDay; w = new Date(w.getFullYear(), w.getMonth(), w.getDate() + 7)) {
              loaded.push(...studioSessions(w).map((e) => ({ ...e, id: `${e.id}-${w.getTime()}` })))
            }
            setEvents(loaded)
            setStatus(
              `Loaded ${loaded.length} events for ${firstDay.toLocaleDateString()} – ${new Date(lastDay.getTime() - 1).toLocaleDateString()} in ${Math.round(performance.now() - started)} ms`
            )
          }, 300)
        }}
        height={560}
        {...handlers}
      />
    </div>
  )
}

About this demo

onPageChange reports the visible range on mount and on every page change; load just that range into data.

Things to try

  • Page forward and watch the status line
  • Switch the span to Month

The props behind it are explained in Loading and saving events.

Use ProScheduler in your project