Javascript 将大日历样式的前几天更改为当前日期
我想根据当前日期设置前几天的背景样式 我试图遵循这一点,但我得到了错误: 不变冲突:React.Children.仅应接收单个React元素子元素 这很疯狂,因为它在示例中起作用。此外,文档中没有关于Javascript 将大日历样式的前几天更改为当前日期,javascript,reactjs,react-big-calendar,Javascript,Reactjs,React Big Calendar,我想根据当前日期设置前几天的背景样式 我试图遵循这一点,但我得到了错误: 不变冲突:React.Children.仅应接收单个React元素子元素 这很疯狂,因为它在示例中起作用。此外,文档中没有关于dateCellWrapper的信息,这没有多大帮助 代码如下: const ColoredDateCellWrapper = (children: any, value: any) => React.cloneElement(Children.only(children), {
dateCellWrapper
的信息,这没有多大帮助
代码如下:
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
}}
/>
const ColoredDateCellWrapper=(子项:any,值:any)=>
React.cloneElement(仅限儿童){
风格:{
…儿童风格,
背景颜色:值
谢谢!:) 代码的第1行出现问题:
const ColoredDateCellWrapper = (children: any, value: any) =>
应该是:
const ColoredDateCellWrapper = ({ children: any, value: any }) =>
简而言之,您将两个参数传递给ColoredDateCellWrapper
,但它只需要1个参数。解构后你应该得到两个道具
按OP要求更新:
如果您不想使用分解,那么可以这样做:
const ColoredDateCellWrapper = (props: any) =>
React.cloneElement(Children.only(props.children), {
style: {
...props.children.style,
backgroundColor: props.value < this.state.currentDay ? 'lightgreen' : 'lightblue',
},
});
constcoloredDateCellWrapper=(道具:any)=>
React.cloneElement(仅限儿童)(道具儿童){
风格:{
…道具、儿童、风格,
背景颜色:props.value
我正在使用typescript,这就是为什么我喜欢它:x@RCohen看看paranthesis里面的花括号。我刚才添加了花括号,typescript抱怨语法如下({children:any,value:any})
非常感谢^^错误不再存在,但它没有应用背景色:S。