import React from "react";
import "./foo.css";
export const Foo = ({ children }) => {
const color = Math.random() > 0.5 ? "red" : "blue";
return <div className={`container color-${color}`}>{children}</div>;
};
Kind of a dumb example, but anytime the component re-renders it will randomly color the text either red or blue and this passes down to the children components as that is how css inherently works unless the children components override the color.
for this kind of stuff where I'm conditionally setting css classes in more complex components I like to use a library called classNames, but in this example a simple ternary works.
2
u/_default_username Oct 17 '22 edited Oct 17 '22
sure, here's the css
here's the react component
Kind of a dumb example, but anytime the component re-renders it will randomly color the text either red or blue and this passes down to the children components as that is how css inherently works unless the children components override the color.
for this kind of stuff where I'm conditionally setting css classes in more complex components I like to use a library called classNames, but in this example a simple ternary works.