反应内联条件

React Inline Condition

我在为我的运费设置在线条件时遇到问题:

cart.shippingPrice =
  country === ("France" || "United States") ? toPrice(20) : toPrice(10);

只有当我select法国条件returns 20,但我希望美国return也一样,有错字吗?

在您的表达式和问题内容中,如果它返回 20,则表示条件为 true

尝试:

cart.shippingPrice = (country === "France" || country === "United States) ? toPrice(20) : toPrice(10)

您需要明确检查每个国家/地区:

country === "France" || country === "United States"

如果您“担心”会有更多国家进入该列表,您可以创建一个数组:

const countries = ["France", "United States"]

然后

countries.includes(country)