question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FullCalendar is not updating after React state change

See original GitHub issue

I am using FullReact calendar (https://fullcalendar.io/docs/react) in my own wrapper component (Calendar):


    const Calendar = (props) => {
      const [isMonthlyViewEnabled, setIsMonthlyViewEnabled] = useState(true);
    
      return (
        <Card fluid>
          <Card.Content>
            <FullCalendar
              defaultView={isMonthlyViewEnabled ? "dayGridMonth" : "timeGridWeek"}
              plugins={[dayGridPlugin, timeGridPlugin]}
              events={props.events}
            />
          </Card.Content>
            <Card.Content extra>
               <Button primary>Add Appointment</Button>
                <Button color={'green'} onClick={() => setIsMonthlyViewEnabled(!isMonthlyViewEnabled)}>
                    Switch to {isMonthlyViewEnabled ? "weekly" : "monthly"} view
                </Button>
            </Card.Content>
        </Card>
      );
    };

Initially, the button will display the text: ‘Switch to weekly view’. Upon click, it will flip isMonthlyViewEnabled and switch the name of the button to ‘Switch to monthly view’ and the defaultView of FullCalendar to “timeGridWeek”.

However, when I click the button, the button text is changing as expected (so I know the state is working correctly), but the FullCalendar default view is not changing. Does anyone know how I can make the FullCalendar component rerender with the proper defaultView?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
rustamfetullayevcommented, Jun 14, 2020

Basically you can use asynchronous function and re-render FullCalendar component. For example if you want to add new event locally:

export const App = () => {
  let [events, setEvents] = React.useState([]);
  let [isAwaiting, setIsAwaiting] = React.useState(false);
  const onSelect = async (data) => {
    let { start, end } = data;
    await setIsAwaiting(true);
    setEvents(
      events.concat({
        title: new Date().getTime(),
        start: moment(start).format("YYYY-MM-DD HH:mm"),
        end: moment(end).format("YYYY-MM-DD HH:mm"),
      })
    );
    setIsAwaiting(false);
  };

  return (
    <div className="App">
      {!isAwaiting && (
        <FullCalendar
          defaultView="timeGridWeek"
          plugins={[dayGridPlugin, timeGridPlugin, interactionPlugin]}
          header={{
            left: "prev,next today",
            center: "title",
            right: "dayGridMonth,timeGridWeek",
          }}
          firstDay={1}
          allDaySlot={false}
          editable
          selectable
          select={onSelect}
          events={events}
          eventLimit
          slotDuration={{ minutes: 10 }}
          slotLabelInterval={{ minutes: 10 }}
          slotLabelFormat={{
            hour: "numeric",
            minute: "2-digit",
            meridiem: false,
            hour12: false,
          }}
          columnHeaderFormat={{ weekday: "long" }}
        />
      )}
    </div>
  );
};
5reactions
ErAz7commented, Apr 25, 2020

in the example above, I wanted to show changing state does not re-render the calendar so I disabled built-in buttons to make it more clear in my real use case, header is different so I prefer to implement it myself to have full control over it instead of dealing with customization. I know I can change view using API via refs (which I already do in my project) but I expected FullCalendar re-render on prop changes (which still makes sense)

and by the way, lemme thank you for this great work, it saves days for devs

Read more comments on GitHub >

github_iconTop Results From Across the Web

FullCalendar is not updating after React state change #7075
I am using FullReact calendar (https://fullcalendar.io/docs/react) in my own wrapper component (Calendar):. const Calendar = (props) => { const ...
Read more >
FullCalendar is not updating after React state change - Reddit
I am using FullReact calendar ( https://fullcalendar.io/docs/react ) in my own wrapper component (Calendar): const Calendar = (props) ...
Read more >
Fullcalendar re-fetch events after change in React state
I believe this happens because every render is processes "events={getCalendarEvents(calendarId)}", which creates new reference to function.
Read more >
Fullcalendar re-fetch events after change in React state-Reactjs
Coding example for the question Fullcalendar re-fetch events after change in React state-Reactjs.
Read more >
@fullcalendar/react - npm
The official React Component for FullCalendar. Latest version: 6.0.1, last published: 5 days ago. Start using @fullcalendar/react in your ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found