Skip to content

Programmatic Control

Control the calendar using the forwarded ref. Pass a ref to the <Calendar /> component and call methods on ref.current.

MethodDescription
getValue(): string | numberGet current date value
setValue(value: string | Date): voidSet date value
reset(): voidClear value and close
setView(view: 'days' | 'months' | 'years'): voidChange calendar view
next(): voidNavigate to next month/year
prev(): voidNavigate to previous month/year
open(): voidOpen calendar popup
close(): voidClose calendar popup
isClosed(): booleanCheck if calendar is closed
update(): voidAccept current selection

Create custom navigation and view controls using useRef():

import React, { useRef, useState } from 'react';
import { Calendar } from '@calendarjs/react';

export default function App() {
    const calendarRef = useRef();
    const [selectedDate, setSelectedDate] = useState(new Date().toISOString().split('T')[0]);

    const goToToday = () => {
        calendarRef.current?.setValue(new Date());
        calendarRef.current?.setView('days');
    };

    return (
        <div>
            <div style={{ marginBottom: '15px', display: 'flex', gap: '10px' }}>
                <button onClick={() => calendarRef.current?.prev()}>Previous</button>
                <button onClick={goToToday}>Today</button>
                <button onClick={() => calendarRef.current?.next()}>Next</button>
                <button onClick={() => calendarRef.current?.setView('days')}>Days</button>
                <button onClick={() => calendarRef.current?.setView('months')}>Months</button>
                <button onClick={() => calendarRef.current?.setView('years')}>Years</button>
                <button onClick={() => calendarRef.current?.reset()}>Reset</button>
            </div>

            <Calendar
                ref={calendarRef}
                type="inline"
                value={new Date()}
                onChange={(value) => setSelectedDate(value)}
            />

            <div style={{ marginTop: '10px' }}>Selected: {selectedDate || 'None'}</div>
        </div>
    );
}

The hook is a drop-in alternative to useRef. The instance it returns is reactive — null before mount, the live instance after — so toolbar buttons can disable themselves until the calendar is ready:

import { useState } from 'react';
import { Calendar, useCalendar } from '@calendarjs/react';

export default function App() {
    const { ref, instance } = useCalendar();
    const [date, setDate] = useState(new Date().toISOString().split('T')[0]);

    return (
        <div>
            <div style={{ display: 'flex', gap: '8px', marginBottom: 12 }}>
                <button disabled={!instance} onClick={() => instance.prev()}>Previous</button>
                <button disabled={!instance} onClick={() => instance.setValue(new Date())}>Today</button>
                <button disabled={!instance} onClick={() => instance.next()}>Next</button>
                <button disabled={!instance} onClick={() => instance.setView('months')}>Months</button>
                <button disabled={!instance} onClick={() => instance.setView('years')}>Years</button>
                <button disabled={!instance} onClick={() => instance.reset()}>Reset</button>
            </div>

            <Calendar ref={ref} type="inline" value={date} onChange={setDate} />

            <div>Selected: {date || 'None'}</div>
        </div>
    );
}