search envelope-o feed check
Home Unanswered Active Tags New Question
user comment-o

navigator is not defined for next js application

Asked by Anonymous
2 days ago.

Error:
Error: Failed to load external module @daypilot/daypilot-lite-react: ReferenceError: navigator is not defined;

Next Version: Next.js v14.0.4
Node Version v18.19.0

’use client’; does not solves the problem. I managed to load resource like:
const DayPilotScheduler = dynamic( () => import('@daypilot/daypilot-lite-react').then(mod => mod.DayPilotScheduler), {ssr: false} );

but I cant get ref or find any way to use scrollTo function for example. Is any workaround to use this function from DayPilot?

Answer posted by Dan Letecky [DayPilot]
2 days ago.

Unfortunately, I’m not able to reproduce the issue with Next.js 14 and 15.

With 'use client';, you should be able to import DayPilotScheduler in a standard way:

import {DayPilot, DayPilotScheduler} from "@daypilot/daypilot-lite-react";

You can also try generating a Next project with the Scheduler component using the UI Builder.

Anyway, there was one navigator reference that was probably causing the problem. You can give it a try with @daypilot/daypilot-lite-react@4.7.0-sandbox.740 - it should be fixed there.

Let me know if the problem persists.

Comment posted by Anonymous
2 days ago.

Yes, with this version i can import it normaly without “dynamic“. For now I will test it with this version. Thanks.
Does
controlRef={setScheduler}
should work in lite version or its pro feature or can it be problem with next js implementation?

Comment posted by Dan Letecky [DayPilot]
2 days ago.

Great, thanks for the update.

Yes, you can use controlRef in the Lite version. It works in Next.js as well:

'use client';

import React, {useEffect, useRef, useState} from "react";
import {DayPilot, DayPilotScheduler} from "@daypilot/daypilot-lite-react";

export default function Scheduler() {

    const [scheduler, setScheduler] = useState<DayPilot.Scheduler>();

    // ...

    const onTimeRangeSelected = async (args: DayPilot.SchedulerTimeRangeSelectedArgs) => {
        const modal = await DayPilot.Modal.prompt("Create a new event:", "Event 1");
        scheduler?.clearSelection();
        if (modal.canceled) {
            return;
        }
        scheduler?.events.add({
            start: args.start,
            end: args.end,
            id: DayPilot.guid(),
            text: modal.result,
            resource: args.resource
        });
    };

    return (
        <div>
            <DayPilotScheduler
                ...
                onTimeRangeSelected={onTimeRangeSelected}
                controlRef={setScheduler}
            />
        </div>
    )
}

You can also use the standard ref but that way you get a reference to the DayPilotScheduler React component, not the DayPilot.Scheduler instance. So the usage would be slightly different:

'use client';

import React, {useEffect, useRef, useState} from "react";
import {DayPilot, DayPilotScheduler} from "@daypilot/daypilot-lite-react";

export default function Scheduler() {

    const schedulerRef = useRef<DayPilotScheduler>(null);

    const onTimeRangeSelected = async (args: DayPilot.SchedulerTimeRangeSelectedArgs) => {
        const modal = await DayPilot.Modal.prompt("Create a new event:", "Event 1");
        schedulerRef.current?.control.clearSelection();
        if (modal.canceled) {
            return;
        }

        schedulerRef.current?.control.events.add({
            start: args.start,
            end: args.end,
            id: DayPilot.guid(),
            text: modal.result,
            resource: args.resource
        });
    };

    return (
        <div>
            <DayPilotScheduler
                ...
                onTimeRangeSelected={onTimeRangeSelected}
                ref={schedulerRef}
            />
        </div>
    )
}
Comment posted by Anonymous
2 days ago.

Worked like a charm, thank you

New Reply
This reply is
Attachments:
or drop files here
Your name (optional):