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

this in undefined in the addEvent example

Related article: React Scheduler Component Tutorial
Asked by Olivier Rossel
3 years ago.

Hello I get a message from my IntelliJ when I try to use the code for the ref attribute and the onTimeRangeSelected attribute. The this in these codes might be uninitialized. And it indeed is undefined at runtime.

Is it something related to using Typescript? or using the DayPilotScheduler inside an fonction-based component and not a class-based component?

Any help is welcome.

Answer posted by Dan Letecky [DayPilot]
3 years ago.

This hint is correct, you can't use "this" in a functional React component. You'll need to create a variable instead:

const Scheduler = () => {
    let scheduler: DayPilot.Scheduler;

    return (
        <div>
            <DayPilotScheduler ref={component => {
              scheduler = component && component.control;
            }} />
        </div>
    );
};
This question is more than 1 months old and has been closed. Please create a new question if you have anything to add.