How to make conditional props in Bootstrap button group in ReactJS
P粉010967136
P粉010967136 2024-02-26 20:47:26
0
1
324

I have useState as follows:

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

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

The code below is ButtonGroup with BootStrap for ReactJS.

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")}
            >

Above, among the props of Button, active causes the button to be selected.

So I set it as the following conditions.

But it will throw an error. Error message: '...'expected.ts(1005)

So I use ...

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

But when I write the code above, it says active props are undefined.

How can I do it?

P粉010967136
P粉010967136

reply all(1)
P粉670107661

You are receiving 'active' is not Defined because you are trying to use active as a variable instead of using it as a property of Button.

Try this

 
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template