admin管理员组文章数量:1429578
I want to conditionally render a button based on two conditions. Am not really sure if am doing it the right way.
But i get this error This condition will always return 'true' since the types '"started"' and '"cancelled"' have no overlap.ts(2367) where the red underline is.
Am using React js with typescript
I want to conditionally render a button based on two conditions. Am not really sure if am doing it the right way.
But i get this error This condition will always return 'true' since the types '"started"' and '"cancelled"' have no overlap.ts(2367) where the red underline is.
Am using React js with typescript
Share Improve this question asked Jul 22, 2020 at 8:36 okumu justineokumu justine 3681 gold badge6 silver badges12 bronze badges 3- So when do you want to show the button? – Shahnawaz Hossan Commented Jul 22, 2020 at 8:39
-
This has nothing to do with React, it's a simple logic error. Your condition will always be true, since
journey
will always be different from either "started" or "cancelled", no matter which value it actually has. You probably want to replace that||
with another&&
– user5734311 Commented Jul 22, 2020 at 8:42 - when journey !== "started" and journey !== "cancelled" is true, because it has about three states (started, cancelled and stopped) – okumu justine Commented Jul 22, 2020 at 8:43
3 Answers
Reset to default 2When you are checking for multiple values, you can simplify with includes
method
{ !["started", "cancelled"].includes(journey) && <Button> Cancel Journey </Button> }
Alternatively
{ journey === "stopped" && <Button> Cancel Journey </Button> }
or
{ ["stopped"].includes(journey) && <Button> Cancel Journey </Button> }
You have a logical error. A variable can not have two values at the same time which means that journey !== "started" || journey !== "cancelled"
will always be true.
You probably want to use journey === "started"
in order to display the Cancel Button
If there are only two options in your condition like "stared" and "cancelled" you can add ternary operator.
{journey === "stared" ? <Button>Journey Started</Button> : <Button>Journey Cancelled </Button>}
But if more options like ("stared","cancelled","pleted","any other")
{journey === "stared" && <Button>Journey Started</Button> }
{journey === "cancelled" && <Button>Journey cancelled</Button> }
{journey === "pleted" && <Button>Journey pleted</Button> }
This is an optional thing, It's a best practice not to use strings directly in conditions. Better you define them as type, or enum,(If using typescript with React) or just costs;
const STARTED = "started"
const COMPLETED = "pleted"
const CANCELED = "canceld"
const journey = STARTED // Or anything the matches with the requirement.
{journey === STARTED ? <Button>Journey Started</Button> : <Button>Journey Cancelled </Button>}
本文标签: javascriptUsing OR operator in React js conditional renderingStack Overflow
版权声明:本文标题:javascript - Using OR operator in React js conditional rendering - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745540543a2662473.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论