如何在 ReactJS 的 Bootstrap 按钮组中制作条件道具
P粉010967136
P粉010967136 2024-02-26 20:47:26
[React讨论组]

我有 useState 如下:

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

并且根据orderStandard的值,我想给出props。

下面的代码是带有ReactJS的BootStrap的ButtonGroup

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

<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 使按钮被选中。

所以我将其设为如下条件。

但是它会抛出错误。错误提示:'...'预期.ts(1005)

所以我用 ...

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

但是当我在上面编写代码时,它说 active props 未定义。

我怎样才能做到?

P粉010967136
P粉010967136

全部回复(1)
P粉670107661

您收到 'active' is not Defined 因为您尝试使用 active 作为变量,而不是将其用作 Button 的属性。

试试这个

 
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号