You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React FullCalendar周视图技术问题:如何将事件注入对应小时单元格

Fix: FullCalendar Events Only Show in All-Day Tab & Mapping to Hourly Cells

Let's break down your issues and fix them step by step, like a fellow developer troubleshooting with you:

1. Why Your Events Are Stuck in the All-Day Tab

The core problem comes down to two critical mistakes in your event data setup:

  • Wrong property names: FullCalendar expects event objects to use start and end (not from and to) to recognize timestamps. Using custom field names means the calendar can't parse your event times.
  • Invalid date format: Your formatting string uses mm (minutes) instead of HH (24-hour time) in the timestamp. Without valid hour data, FullCalendar assumes all events are "all-day" by default.

2. Fixing the Event Data Format

First, update your callendarEvents mapping to align with FullCalendar's requirements. Here's the corrected version:

import { format, isDate } from "date-fns"; // Add isDate for better validation

// ...

const callendarEvents = lessons.map(({ from, to }) => {
  // Skip entries with missing dates
  if (!from || !to) return null;

  const startDate = new Date(from);
  const endDate = new Date(to);

  // Validate dates are actually valid
  if (!isDate(startDate) || !isDate(endDate) || isNaN(startDate.getTime()) || isNaN(endDate.getTime())) {
    console.error(`Skipping invalid event: from=${from}, to=${to}`);
    return null;
  }

  // Use FullCalendar's expected properties and correct ISO timestamp format
  return {
    start: format(startDate, "yyyy-MM-dd'T'HH:mm:ss"), // HH = 24-hour format for hours
    end: format(endDate, "yyyy-MM-dd'T'HH:mm:ss"),
    title: "event",
    allDay: false // Explicitly mark as non-all-day (optional but eliminates ambiguity)
  };
}).filter(event => event !== null); // Remove any invalid events from the array

3. Clean Up FullCalendar Props

You're currently passing both events={lessons} and initialEvents={callendarEvents} to the calendar, which creates conflicts. Replace the raw lessons data with your processed event array:

<FullCalendar
  // ... all your existing props
  events={callendarEvents} // Use the corrected, FullCalendar-compatible events
  // Remove initialEvents={callendarEvents} since we're feeding events directly
/>

4. Mapping Events to Hourly Cells (No Extra Hooks Needed!)

Once your event data has valid start/end timestamps with hour information, FullCalendar will automatically render events in the correct hourly cells of the timeGridWeek/timeGridDay views. This is built-in behavior—you don't need special hooks for this.

The eventDataTransform hook you mentioned is for tweaking event data before processing, but your main issue wasn't transformation logic—it was incorrect data structure and formatting.

5. Quick Verification Checks

  • Switch to the "Week" or "Day" view (you have these enabled in headerToolbar, so they should be visible in the top right).
  • Check your browser console for date parsing errors—if any pop up, double-check that your lessons data's from/to values are valid date strings or timestamps.
  • Confirm your DateButton component is setting valid time values that flow correctly into your event data (if it's tied to adding/editing events).

Bonus: Improve Your Date Validation Helper

Update your parseDate function to use date-fns' isDate for more reliable validation:

function parseDate(date) {
  const parsedDate = new Date(date);
  if (!isDate(parsedDate) || isNaN(parsedDate.getTime())) {
    console.error(`Date: ${date} is not a valid date`);
    return;
  }
  return format(parsedDate, "yyyy-MM-dd");
}

After making these changes, your events should show up in the correct hourly cells instead of being stuck in the all-day tab.

内容的提问来源于stack exchange,提问作者seven

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 08:38:12