我正在尝试使用动态数据来更改和触发类名,但尽管数据包含 false,它总是只会导致 true。有什么解决办法吗?

问题描述 投票:0回答:1

我正在尝试根据三元类名动态更改样式。 我正在尝试根据此处 workWrapper 上的输入将背景更改为深色或浅色: I have a Data.js wherein I can just put every data in one file

Which is connected to this component

and I am basing it to here

However, I cannot figure out why the dark classname is not being triggered when I already set the workWrapper value to false/true, it is still light

CSS:

.normColor{
  background-color: #f3f3f3;
  width: 100%;
  height: 100vh;
  margin: 0 auto;
}

.normColor .dark{
  background-color: #232A4E;
  width: 100%;
  height: 100vh;
  margin: 0 auto;
}

我已经尝试过弄乱该组件,搜索其他与我类似的问题并尝试搜索有关它的 youtube 视频,但我找不到任何解决方案。

css reactjs components ternary
1个回答
0
投票

你忘了解构你的道具。

试试这个:-

const WorkSection = ({workWrapper}) =>{
    return(<div className={workWrapper?'normColor':'dark'}></div>)
}

然后你检查 props,它是否为空。它总是会给你真实的,因为组件总是有 props

© www.soinside.com 2019 - 2024. All rights reserved.