React JS将CSS转换为CSS以进行反应

问题描述

我在reactjs中有以下项目,带有以下外部CSS代码,我想将CSS代码转换为CSS样式以在函数内部使用。

这是我设法转换的第一部分,但是我在弄清楚如何转换其余部分以及如何在Toggle方法中使用它们方面遇到困难。

链接:codesandbox

P.s。 我不明白为什么切换不正确时您为什么看不到。

const styles = {
        toggle: {
            height: "100px",width: "200px",borderRadius: "50px",margin: "auto",backgroundImage: "linear-gradient(aqua,skyblue)",position: "relative",cursor: "pointer",},notch: {
            height: "90px",width: "90px",borderRadius: "50%",background: "yellow",position: "absolute",top: "5px",left: "5px",boxShadow: "0 0 5px yellow",zIndex: 1,transition: "all 0.3s ease",}
    };
const {useState} = React;

const Toggle = ({ toggled,onClick }) => {
    return (
        <div onClick={onClick} className={`toggle${toggled && " night"}`}>
            <div className="notch">
                <div className="crater" />
                <div className="crater" />
            </div>
            <div>
                <div className="shape sm" />
                <div className="shape sm" />
                <div className="shape md" />
                <div className="shape lg" />
            </div>
        </div>
    );
}

const App = () => {
  const [toggled,setToggled] = useState(true);
    const handleClick = () => setToggled((s) => !s);
    return (
        <div className="App">
            <Toggle toggled={toggled} onClick={handleClick} />
        </div>
    );
};

ReactDOM.render(
  <App />,document.getElementById("react")
);
.toggle {
    height: 100px;
    width: 200px;
    border-radius: 50px;
    margin: auto;
    background-image: linear-gradient(aqua,skyblue);
    position: relative;
    cursor: pointer;
}

.toggle.night {
    background-image: linear-gradient(midnightblue,rebeccapurple);
}

.notch {
    height: 90px;
    width: 90px;
    border-radius: 50%;
    background: yellow;
    position: absolute;
    top: 5px;
    left: 5px;
    box-shadow: 0 0 5px yellow;
    z-index: 1;
    transition: all 0.3s ease;
}

.notch > .crater {
    background: burlywood;
    border-radius: 50%;
    position: absolute;
    opacity: 0;
    box-shadow: 0 5px 5px rgba(0,0.4) inset;
}
.night .crater {
    opacity: 0.4;
}

.crater:first-child {
    left: 5px;
    top: 15px;
    height: 15px;
    width: 40px;
    transform: rotate(-45deg);
}

.crater:last-child {
    right: 10px;
    top: 15px;
    height: 15px;
    width: 25px;
    transform: rotate(45deg);
}

.night > .notch {
    background: whitesmoke;
    box-shadow: 0 0 5px whitesmoke;
    transform: translate(100px,0);
}

.shape {
    position: absolute;
    background: whitesmoke;
    border-radius: 50%;
    transition: all 0.3s ease;
}

.shape.sm {
    height: 5px;
    width: 50px;
    top: 50%;
    left: 60%;
}

.shape.md {
    height: 10px;
    width: 75px;
    top: 25%;
    left: 25%;
    z-index: 2;
}

.shape.lg {
    height: 15px;
    width: 100px;
    bottom: 20px;
    left: 25%;
}

.night .shape {
    background: lightgray;
    box-shadow: 0 0 10px 2px violet;
}

.night .shape.sm {
    height: 5px;
    width: 5px;
    transform: translate(-40px,0);
}

.night .shape.sm:first-of-type {
    transform: translate(-80px,-10px);
}

.night .shape.md {
    height: 10px;
    width: 10px;
    transform: translate(10px,0);
}

.night .shape.lg {
    height: 15px;
    width: 15px;
    transform: translate(-10px,0);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<div id="react"></div>

解决方法

动态className的值应为“ toggle day”或“ toggle night”,因此创建了一个变量“ dayNightClass”来保存该值,该值取决于toggled的值。除此之外,都是一样的。

.toggle {
    height: 100px;
    width: 200px;
    border-radius: 50px;
    margin: auto;
    background-image: linear-gradient(aqua,skyblue);
    position: relative;
    cursor: pointer;
}

.toggle.night {
    background-image: linear-gradient(midnightblue,rebeccapurple);
}

.notch {
    height: 90px;
    width: 90px;
    border-radius: 50%;
    background: yellow;
    position: absolute;
    top: 5px;
    left: 5px;
    box-shadow: 0 0 5px yellow;
    z-index: 1;
    transition: all 0.3s ease;
}

.notch > .crater {
    background: burlywood;
    border-radius: 50%;
    position: absolute;
    opacity: 0;
    box-shadow: 0 5px 5px rgba(0,0.4) inset;
}
.night .crater {
    opacity: 0.4;
}

.crater:first-child {
    left: 5px;
    top: 15px;
    height: 15px;
    width: 40px;
    transform: rotate(-45deg);
}

.crater:last-child {
    right: 10px;
    top: 15px;
    height: 15px;
    width: 25px;
    transform: rotate(45deg);
}

.night > .notch {
    background: whitesmoke;
    box-shadow: 0 0 5px whitesmoke;
    transform: translate(100px,0);
}

.shape {
    position: absolute;
    background: whitesmoke;
    border-radius: 50%;
    transition: all 0.3s ease;
}

.shape.sm {
    height: 5px;
    width: 50px;
    top: 50%;
    left: 60%;
}

.shape.md {
    height: 10px;
    width: 75px;
    top: 25%;
    left: 25%;
    z-index: 2;
}

.shape.lg {
    height: 15px;
    width: 100px;
    bottom: 20px;
    left: 25%;
}

.night .shape {
    background: lightgray;
    box-shadow: 0 0 10px 2px violet;
}

.night .shape.sm {
    height: 5px;
    width: 5px;
    transform: translate(-40px,0);
}

.night .shape.sm:first-of-type {
    transform: translate(-80px,-10px);
}

.night .shape.md {
    height: 10px;
    width: 10px;
    transform: translate(10px,0);
}

.night .shape.lg {
    height: 15px;
    width: 15px;
    transform: translate(-10px,0);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<div id="react"></div>
var query = dbContext.Complaint                         // To get the Complaints table 
                     .Include(c => c.ComplaintStatus)   // To get the ComplaintStatus table
                     .Include(s => d.ComplaintStatus.Select(st => st.Status))       // To get the Status table
                     .AsQueryable();

var complaintId = 0;
var statusId = 0; 

// As per my research we need to check if the filters variables has values or return all 
// if we do not have filters in place.
// For this,I check if any filter has a value different than 0 and also re-check in the where 
// clause if I need to filter by that specific item

if (complaintId || statusId)
{
    query = query.Where(x => (
                // This returns perfectly                   
                (complaintId && x.Id.Equals(complaintId))      

                // This is where I'm totally stuck with no clue on how to search for the last Complaint Status equal to the designated statusId

                || (statusId && x.Status.Any(y => (y.Status == statusId && y.Active == true)))
}

// Finally I fire the DB request 
var response = query.ToListAsync();

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...