我需要根據傳入我的反應組件的道具來設置div的背景顏色。 React組件的內聯樣式我非常清楚,但我不知道如何正確應用內聯樣式以根據prop來更改。如果道具selected
等於true
,我只想在right-toggle
的聯機樣式中分配道具rightSideColor
的值。條件內聯樣式基於道具的反應組件
export default function UiToggle(props) {
const { leftLabel, rightLabel, selected, rightSideColor, leftSideColor } = props;
return (
<div className="lr-toggle-select" style={{ width: `${width}px` }} >
<div className="lr-gray-background" />
<div>
{leftLabel}
</div>
<div className={'lr-toggle right-toggle' style={{ selected ? (backgroundColor: rightSideColor) : null }}>
{rightLabel}
</div>
</div>
);
}