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

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

How to make conditional props in Bootstrap Button Group in ReactJS

问题

有了下面这个 useState

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

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

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

  1. <ButtonGroup
  2. style={{
  3. height: 35,
  4. display: "flex",
  5. justifyContent: "center",
  6. alignItems: "center",
  7. }}
  8. >
  9. <Button
  10. id="order-button"
  11. variant="light"
  12. style={{ width: 80 }}
  13. active
  14. onClick={() => setOrderStandard("total")}
  15. >

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

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

  1. <Button
  2. id="order-button"
  3. variant="light"
  4. style={{ width: 80 }}
  5. {...(orderStandard === "total" ? active : null)}
  6. onClick={() => setOrderStandard("total")}
  7. >

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

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

  1. <Button
  2. id="order-button"
  3. variant="light"
  4. style={{ width: 80 }}
  5. {...(orderStandard === "total" ? { active } : null)}
  6. onClick={() => setOrderStandard("total")}
  7. >

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

如何解决呢?

英文:

I have useState as below:

  1. 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

  1. &lt;ButtonGroup
  2. style={{
  3. height: 35,
  4. display: &quot;flex&quot;,
  5. justifyContent: &quot;center&quot;,
  6. alignItems: &quot;center&quot;,
  7. }}
  8. &gt;
  9. &lt;Button
  10. id=&quot;order-button&quot;
  11. variant=&quot;light&quot;
  12. style={{ width: 80 }}
  13. active
  14. onClick={() =&gt; setOrderStandard(&quot;total&quot;)}
  15. &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 ...

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

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

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

How can I make it?

答案1

得分: 1

试试这样写:

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

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

英文:

Try it like this:

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

If he scolds false you can try with null and undefined

答案2

得分: 1

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

尝试使用这个代替:

  1. &lt;Button
  2. id=&quot;order-button&quot;
  3. variant=&quot;light&quot;
  4. style={{ width: 80 }}
  5. active={orderStandard == &quot;total&quot;}
  6. onClick={() =&gt; setOrderStandard(&quot;total&quot;)}
  7. &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

  1. &lt;Button
  2. id=&quot;order-button&quot;
  3. variant=&quot;light&quot;
  4. style={{ width: 80 }}
  5. active={orderStandard == &quot;total&quot;}
  6. onClick={() =&gt; setOrderStandard(&quot;total&quot;)}
  7. &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:

确定