如何在ReactJS中使用Bootstrap按钮组制作有条件的属性

huangapple go评论64阅读模式
英文:

How to make conditional props in Bootstrap Button Group in ReactJS

问题

有了下面这个 useState

const [orderStandard, setOrderStandard] = useState("total");

根据 orderStandard 的值,我想要传递 props。

这段代码是使用 ReactJS 的 BootStrap 中的 ButtonGroup

<ButtonGroup
  style={{
    height: 35,
    display: "flex",
    justifyContent: "center",
    alignItems: "center",
  }}
>
  <Button
    id="order-button"
    variant="light"
    style={{ width: 80 }}
    active
    onClick={() => setOrderStandard("total")}
  >

上面,在 Button 的 props 中,active 会使按钮被选中。

所以我将其做成条件语句,如下所示:

<Button
  id="order-button"
  variant="light"
  style={{ width: 80 }}
  {...(orderStandard === "total" ? active : null)}
  onClick={() => setOrderStandard("total")}
>

然而,这会抛出错误。错误信息是:'...' expected.ts(1005)

所以我使用了 ...,像这样:

<Button
  id="order-button"
  variant="light"
  style={{ width: 80 }}
  {...(orderStandard === "total" ? { active } : null)}
  onClick={() => setOrderStandard("total")}
>

但当我写了上面的代码时,它说 active props 未定义。

如何解决呢?

英文:

I have useState as below:

 const [orderStandard, setOrderStandard] = useState(&quot;total&quot;);

And according to the value of orderStandard, I want to give props.

This below code is ButtonGroup of BootStrap with ReactJS.

https://react-bootstrap.github.io/components/button-group/#button-toolbar-props

   &lt;ButtonGroup
            style={{
              height: 35,
              display: &quot;flex&quot;,
              justifyContent: &quot;center&quot;,
              alignItems: &quot;center&quot;,
            }}
          &gt;
            &lt;Button
              id=&quot;order-button&quot;
              variant=&quot;light&quot;
              style={{ width: 80 }}
              active
              onClick={() =&gt; setOrderStandard(&quot;total&quot;)}
            &gt;

above, among props of Button, active make button being selected.

So I make it conditional as below.

如何在ReactJS中使用Bootstrap按钮组制作有条件的属性

However it throws error. Error says: &#39;...&#39; expected.ts(1005)

So I make with ...

 &lt;Button
                  id=&quot;order-button&quot;
                  variant=&quot;light&quot;
                  style={{ width: 80 }}
                  {...(orderStandard == &quot;total&quot; ? active : null)}
                  onClick={() =&gt; setOrderStandard(&quot;total&quot;)}
                &gt;

But when I wrote code above, It says active props is not defined.

如何在ReactJS中使用Bootstrap按钮组制作有条件的属性

How can I make it?

答案1

得分: 1

试试这样写:

active={orderStandard === "total" ? true : false)}

如果他返回 false,你可以尝试使用 nullundefined

英文:

Try it like this:

active={orderStandard == &quot;total&quot; ? true : false)}

If he scolds false you can try with null and undefined

答案2

得分: 1

你出现'active' 未定义的错误是因为你尝试将active作为变量使用,而不是将其作为Button的属性使用。

尝试使用这个代替:

 &lt;Button
  id=&quot;order-button&quot;
  variant=&quot;light&quot;
  style={{ width: 80 }}
  active={orderStandard == &quot;total&quot;}
  onClick={() =&gt; setOrderStandard(&quot;total&quot;)}
 &gt;
英文:

You're getting &#39;active&#39; is not defined because you're trying to use active as a variable instead of using it as an attribute of Button.

Try this instead

 &lt;Button
  id=&quot;order-button&quot;
  variant=&quot;light&quot;
  style={{ width: 80 }}
  active={orderStandard == &quot;total&quot;}
  onClick={() =&gt; setOrderStandard(&quot;total&quot;)}
 &gt;

huangapple
  • 本文由 发表于 2023年3月9日 23:00:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686341.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定