Search code examples
cssreactjscomponentsternary

I am trying to use dynamic data to change and trigger classnames but it always only result to true despite data containing false. Any solution?


I am trying to dynamically change the styles based on ternary classnames. I am trying to change the background to dark or light based on the input on the workWrapper in here: 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

The CSS:

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

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

I already tried messing around with the component, searching for other similar issues to mine and trying to search youtube videos regarding it but I cannot find any solution.


Solution

  • You forget to de-structure your props.

    try this :-

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

    And you checking props, if it is null or not. And it will always give you true, because components always have props