--- title: React Adapter id: react-adapter --- # React Adapter `@tanstack/react-time` is a thin wrapper around `@tanstack/time` that turns a calendar core into a React hook. ## `useCalendar` The `useCalendar` hook creates a calendar instance and subscribes its store to your component. ```tsx import { useCalendar } from '@tanstack/react-time' import { calendarFeatures, stockFeatures } from '@tanstack/time' function MyCalendar() { const calendar = useCalendar({ viewMode: { value: 1, unit: 'week' }, timeZone: 'UTC', features: calendarFeatures(stockFeatures), events: [ { id: '1', title: 'Team Standup', start: '2024-03-18T09:00:00', end: '2024-03-18T10:00:00', }, ], }) return (
) } ``` ## Move & Resize Controllers Compose `eventMoveFeature` and `eventResizeFeature` (both require `eventRecurrenceFeature`) and the hook exposes controller helpers. Snapping is set with `constraints.snapToMinutes`; `containerHeight` is the pixel height of one day column. ```tsx import { useEffect, useRef } from 'react' import { useCalendar } from '@tanstack/react-time' import { calendarFeatures, eventMoveFeature, eventRecurrenceFeature, eventResizeFeature, } from '@tanstack/time' import type { Event } from '@tanstack/time' const DAY_HEIGHT_PX = 1152 function DraggableCalendar({ events }: { events: Array }) { const calendar = useCalendar({ viewMode: { value: 1, unit: 'week' }, timeZone: 'UTC', features: calendarFeatures([eventRecurrenceFeature, eventMoveFeature, eventResizeFeature]), move: { containerHeight: DAY_HEIGHT_PX, constraints: { snapToMinutes: 15 } }, resize: { containerHeight: DAY_HEIGHT_PX, constraints: { snapToMinutes: 15 } }, events, }) const { moveState, updateEventMove, endEventMove, cancelEventMove } = calendar const startY = useRef(0) useEffect(() => { if (!moveState.isMoving) return const onPointerMove = (e: PointerEvent) => { const column = document .elementFromPoint(e.clientX, e.clientY) ?.closest('[data-day]') updateEventMove({ dayDate: column?.dataset.day, deltaPixels: e.clientY - startY.current }) } window.addEventListener('pointermove', onPointerMove) window.addEventListener('pointerup', endEventMove) return () => { window.removeEventListener('pointermove', onPointerMove) window.removeEventListener('pointerup', endEventMove) } }, [moveState.isMoving, updateEventMove, endEventMove, cancelEventMove]) return calendar.days.map((day) => (
{day.events.map((event) => (
{ const { originalStart, originalEnd } = calendar.getEventSegmentInfo(event) startY.current = e.clientY calendar.startEventMove({ eventId: event.id, originalStart, originalEnd, dayDate: day.isoDate, }) }} > {event.title}
))}
)) } ``` `startEventMove` only begins a move: drive it with `updateEventMove` and finish with `endEventMove` or `cancelEventMove`. See [Move, Resize & Layout](../../interaction) for the controller model and a full example in `examples/react/calendar-drag-resize`. ## Devtools If you have TanStack Devtools installed, register the time plugin to inspect calendar state and operations. ```tsx import { TanStackDevtools } from '@tanstack/react-devtools' import { timeDevtoolsPlugin } from '@tanstack/react-time-devtools' function App() { return ( <> ) } ``` ## API Reference See the [React API Reference](./reference/index) for the full list of hooks, types, and helpers.