JavaScript 表达式无法理解

标签 javascript operators jsx

我是 React 和 Js 的新手,我想了解这行代码(它是 JSX 内部的 Js):

<h5 className="recipes__title">
  {item.recipe.label < 20 ? `${item.recipe.label}` : `${item.recipe.label.substring(0, 25)}...` }
</h5>

有人知道如何阅读和理解它吗?

谢谢!

最佳答案

<h5 className="recipes__title">            //An html header
                                           //Containing... 
  {
    item.recipe.label < 20 ?               // If the item.recipe.label is less than 20 then...

`${item.recipe.label}`                     // the label
: `${item.recipe.label.substring(0, 25)}   //else the first 25 characters of the label followed by 
  ...`                                     // the string "..."     
}
</h5>

您可以了解三元运算符(这是有条件解析为两个表达式之一的表达式)here

您可以找到有关模板文字(可以包含要解析的 javascript 的字符串)here

关于JavaScript 表达式无法理解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60599380/

相关文章:

javascript - 在自定义 js 上传递给 getElementById() 的空字符串

operators - 在csh中,为什么4 - 3 + 1 == 0?

operators - 什么是原始 Forth 运算符?

Java数学运算简写?

javascript - 无法调用未定义node.js的方法 'forEach'

javascript - 定位动态添加的视频

javascript - 如何修复 babel react "Uncaught SyntaxError: Unexpected token <"

javascript - 无法使用 React 和 Redux 从自定义组件更新应用程序状态

javascript - ReactJS 将方法绑定(bind)到类组件

javascript - 发送表单 POST 时不带参数