简体   繁体   English

有条件地内联样式基于 prop 的反应组件

[英]Conditionally inline style a react component based on prop

I need to set the background color of a div based on a prop passed into my react component.我需要根据传递给我的反应组件的道具设置 div 的背景颜色。 Inline styling of React components I am pretty clear on, but I don't know how to correctly apply the inline style to change depending on a prop. React 组件的内联样式我很清楚,但我不知道如何正确应用内联样式以根据道具进行更改。 I only want to assign the value of the prop rightSideColor in the inline styling of right-toggle if the prop selected is equal true .如果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>
  );
}

I would suggest placing all styling and also the conditional operator in a separate const. 我建议将所有样式和条件运算符放在一个单独的const中。

export default function UiToggle(props) {

  const { leftLabel, rightLabel, selected, rightSideColor, leftSideColor } = props;

  const rightToggleStyle = {
     backgroundColor: selected ? rightSideColor : null
  };

  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={rightToggleStyle}>
        {rightLabel}
      </div>
    </div>
  );
}

I would try to do the same with the styling of the width. 我会尝试对宽度的样式做同样的事情。 Good luck! 祝好运!

Fixed a typo - { before className 修正了一个拼写错误 - { className之前

and you can return an empty object if selected is false else the expected value 并且如果selected为false,则可以返回空对象,否则返回预期值

Example: 例:

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} : {} }}>
        {rightLabel}
      </div>
    </div>
  );
}

You can conditionally set the value of attributes like style, override them using rules of precedence, and determine whether to include them at all. 您可以有条件地设置样式的属性值,使用优先级规则覆盖它们,并确定是否包含它们。

export default function UiToggle(props) {
  const { leftLabel, rightLabel, selected, rightSideColor, leftSideColor } = props;
  //specify style and id (and any other attributes) or don't.
  const attrs = selected ? { style: { backgroundColor: "rightSideColor" },id:"hi123" }:{}
  //Conditionally override the class names if we want:
  if (props.className) attrs.className = props.className

  return (
    <div className="lr-toggle-select" style={{ width: `${width}px` }} >
      <div className="lr-gray-background" />
      <div>
        {leftLabel}
      </div>

      {/*Use the spread operator to apply your attributes from attr*/}
      {/*Note that the 'id' set below can't be overridden by attrs whereas*/}
      {/*className will be. That's because precedence goes from right to left.*/}           
      {/*Rearrange them to get what you want.*/}
      {/*Funky comment format is to make valid JSX and also make SO formatter happy*/}          

      <div className='lr-toggle right-toggle' {...attrs} id="attrs_cant_override_this_because_its_on_the_right">
        {rightLabel}
      </div>
    </div>
  );
}

Try something like this:尝试这样的事情:

   <div className='lr-toggle right-toggle' style={ selected ? {backgroundColor: rightSideColor} : '' }}>
    {rightLabel}
  </div>

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

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