4

I would like to styles the background of previous days according to the current date.

I'm trying to follow this example here, but I get the error:

Invariant Violation: React.Children.only expected to receive a single React element child.

Which is crazy, because it works in the example. Also, there is no information about the dateCellWrapper in the docs, which doesn't help much.

Here is the code:

const ColoredDateCellWrapper = (children: any, value: any) =>
    React.cloneElement(Children.only(children), {
        style: {
            ...children.style,
            backgroundColor: value < this.state.currentDay ? 'lightgreen' : 'lightblue',
        },
    });

<BigCalendar
    showMultiDayTimes
    localizer={localizer}
    selectable
    selected={this.state.selected}
    onSelectEvent={this.onSelectEvent}
    onSelectSlot={this.onSelectSlot}
    events={this.state.events}
    step={60}
    timeslots={1}
    defaultView='week'
    startAccessor="start"
    endAccessor="end"
    defaultDate={new Date()}
    components={{
        dateCellWrapper: ColoredDateCellWrapper
    }}
/>

Thank you! :)

Tholle
  • 108,070
  • 19
  • 198
  • 189
RCohen
  • 1,702
  • 10
  • 24
  • 44

1 Answers1

1

There is a problem in 1st line of your code:

const ColoredDateCellWrapper = (children: any, value: any) =>

It should be:

const ColoredDateCellWrapper = ({ children: any, value: any }) =>

In short you are passing two arguments to ColoredDateCellWrapper, but it expects only 1. After destructuring you should get two props.

Update as asked by OP:

If you don't want to use destructuring, then you can do it like this:

const ColoredDateCellWrapper = (props: any) =>
React.cloneElement(Children.only(props.children), {
    style: {
        ...props.children.style,
        backgroundColor: props.value < this.state.currentDay ? 'lightgreen' : 'lightblue',
    },
});
Vishal
  • 6,238
  • 10
  • 82
  • 158