简体   繁体   English

如何使用带有“$$typeof”的 React object:Symbol(react.element) 添加 CSS 类

[英]How to use a React object with "$$typeof": Symbol(react.element) to add CSS classes

I'm trying to add a conditional CSS class to each day of the calendar: https://material-ui-pickers.dev/api/DatePicker我正在尝试向日历的每一天添加条件 CSS class: https://material-ui-pickers.dev/api/DatePicker

This prop seems to be the way:这个道具似乎是这样的:

renderDay(day: DateIOType, selectedDate: DateIOType, dayInCurrentMonth: boolean, dayComponent: Element) => Element

... but I'm not sure how to work with the dayComponent parameter ...但我不确定如何使用dayComponent参数

handleRenderDay = (day, selectedDate, dayInCurrentMonth, dayComponent) => {
    console.log(dayComponent)
    return dayComponent
}

When I console.log dayComponent , it's a simple object with "$$typeof": Symbol(react.element) as an attribute.当我 console.log dayComponent时,它是一个简单的 object,带有 "$$typeof": Symbol(react.element) 作为属性。 How can I modify this object to add a CSS class to the element when the component is rendered?我如何修改这个 object 以在组件渲染时向元素添加一个 CSS class?

Edit: React.isValidElement(dayComponent) returns true so that answers "What is this object?"编辑: React.isValidElement(dayComponent)返回 true 以便回答“这是什么 object?” that I was wondering about.我想知道的。

Ok, this question does help: React.cloneElement -> Add className element好的,这个问题确实有帮助: React.cloneElement -> Add className element

Thanks for that @bertdida感谢@bertdida

I found out that the object I was wondering about is a valid React element so I'm cloning it and returning the new one which works.我发现我想知道的 object 是一个有效的 React 元素,所以我正在克隆它并返回有效的新元素。

handleRenderDay = (day, selectedDate, dayInCurrentMonth, dayComponent) => {
    console.log(React.isValidElement(dayComponent)) // this is a valid element
    const newDayComponent = React.cloneElement(dayComponent, {
        className: "exclaim" // this clobbers it's classes
    })
    console.log(dayComponent.props.className) // this is undefined
    return newDayComponent
}

The problem now is to preserve the existing classes and add one to them.现在的问题是保留现有的类并向其中添加一个。 With the above code, the days have now lost all their styling.有了上面的代码,日子现在已经失去了所有的样式。

Alternatively, I can wrap the dayComponent with a div with my class so the original classes and element are preserved:或者,我可以用我的 class 将dayComponent包装在一个 div 中,以便保留原始类和元素:

handleRenderDay = (day, selectedDate, dayInCurrentMonth, dayComponent) => {
    return (
        <div className="exclaim">
            {dayComponent}
        </div>
    )
}

I'm still learning React so I can't help thinking there should be a way to add (not clobber) classes to the element thereby preserving the intended styles.我仍在学习 React,所以我不禁想到应该有一种方法可以向元素添加(而不是破坏)类,从而保留预期的 styles。

Here is the doc that shows usage of the renderDay prop: https://material-ui-pickers.dev/demo/datepicker#dynamic-data这是显示renderDay道具用法的文档: https://material-ui-pickers.dev/demo/datepicker#dynamic-data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM