简体   繁体   English

变量 css 不适用于反应 js

[英]variable css not applying in react js

this is a react app.这是一个反应应用程序。 my css class is not changing even though the state assign to track the boolean value is changing.我的 css class 没有改变,即使 state 分配给跟踪 Z84E2C64F38F78BA27EA5C905AB52A 值正在改变。 I've passed my states accordingly for this still not working.我已经相应地通过了我的州,因为这仍然不起作用。 here is the github url这是 github url

my app.js file -我的 app.js 文件 -

import React, { useState } from "react";

// Import Styles
import "./styles/App.css";

// Import Components
import Hello from "./components/Hello";
import Rectangle from "./components/Rectangle";
import Button from "./components/Button";

function App() {
  const [colorStatus, setColorStatus] = useState(false);

  return (
    <div className="App">
      <Hello />
      <Rectangle colorStatus={colorStatus} />
      <Button colorStatus={colorStatus} setColorStatus={setColorStatus} />
    </div>
  );
}

export default App;

button.js -\按钮.js -\

import React from "react";

const Button = ({ colorStatus, setColorStatus }) => {
  return (
    <div className="button">
      <button className="btn-1" onClick={() => setColorStatus(!colorStatus)}>
        Press
      </button>
    </div>
  );
};

export default Button;

rectangle.js -矩形.js -

import React from "react";

const Rectangle = ({colorStatus}) => {
  return <div className={`rectangle ${colorStatus ? 'active-rectangle' : '' }`}></div>;
};

export default Rectangle;

necessary css -必要的 css -

.rectangle {
  width: 50vw;
  height: 50vh;
  background: rgba(255, 0, 0, 0.4);
  position: fixed;
  top: 30%;
  left: 35%;
}

.active-rectangle {
  width: 50vw;
  height: 50vh;
  background: rgba(0, green, 0, 0.4);
  position: fixed;
  top: 30%;
  left: 35%;
}

Your .active-rectangle css-rule needs correction like so:-您的.active-rectangle css-rule 需要像这样更正:-

.active-rectangle {
  width: 50vw;
  height: 50vh;
  background: rgba(0, 255, 0, 0.4);
  position: fixed;
  top: 30%;
  left: 35%;
}

So your state should be getting updated and the active-rectangle class is getting applied.所以你的 state 应该得到更新并且active-rectangle class 得到应用。 Just the background property isn't getting applied properly.只是background属性没有得到正确应用。 Rest must still be getting applied. Rest 必须仍在应用中。

background isn't getting applied properly because you cannot use green when a decimal value is expected inside rgba(...) . background没有得到正确应用,因为当rgba(...)内需要decimal值时,您不能使用green

Actually your css class is getting changed (according to the url you provided).实际上,您的 css class 正在发生变化(根据您提供的 url)。

Problem seems to be with the usage of background: rgba(0, green, 0, 0.4) .问题似乎与background: rgba(0, green, 0, 0.4) Specifically in rgba(0, green, 0, 0.4) .特别是在rgba(0, green, 0, 0.4)中。

Instead of green , it should have been a number from 0 to 255 to denote the green part of the colour.而不是green ,它应该是一个从 0 到 255 的数字来表示颜色的绿色部分。 G of RGBA from rgba function you are using您正在使用的来自rgba function 的 RGBA 的 G

checkout https://cssreference.io/property/background-color/结帐https://cssreference.io/property/background-color/

The problem, as said here before is the background property如前所述,问题是背景属性

Lets refactor the code a bit while we are in it.让我们在其中重构代码。

.rectangle {
  width: 50vw;
  height: 50vh;
  background: rgba(255, 0, 0, 0.4);
  position: fixed;
  top: 30%;
  left: 35%;
}

.rectangle.active {
  background: rgba(0, 255, 0, 0.4);
}
import React from "react";

const Rectangle = ({colorStatus}) => {
  return <div className={`rectangle ${colorStatus ? 'active' : '' }`}></div>;
};

export default Rectangle;

Sandbox: https://codesandbox.io/s/friendly-galileo-gwzbh?file=/src/components/Rectangle.js沙盒: https://codesandbox.io/s/friendly-galileo-gwzbh?file=/src/components/Rectangle.js

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

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