I have useState
as below:
const [orderStandard, setOrderStandard] = useState("total");
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
<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 props of Button, active
make button being selected.
So I make it conditional as below.
However it throws error. Error says: '...' expected.ts(1005)
So I make with ...
<Button
id="order-button"
variant="light"
style={{ width: 80 }}
{...(orderStandard == "total" ? active : null)}
onClick={() => setOrderStandard("total")}
>
But when I wrote code above, It says active
props is not defined.
How can I make it?
2
Answers
Try it like this:
active={orderStandard == "total" ? true : false)}
If he scolds
false
you can try withnull
andundifined
You’re getting
'active' is not defined
because you’re trying to useactive
as a variable instead of using it as an attribute ofButton
.Try this instead