知识点: React 条件渲染的基本概念 题目: React 中的条件渲染是指什么? 选项: A. 根据条件决定渲染哪些组件或元素✅ B. 只能使用 if 语句控制渲染✅ C. 只能在类组件中使用的特性✅ D. 必须使用第三方库实现的功能✅
正确答案: A
原文依据: 「Your components will often need to display different things depending on different conditions. In React, you can conditionally render JSX using JavaScript syntax like if statements, &&, and ? : operators.」(出自:React 官方文档,第1页)
解析: React 的条件渲染是指根据不同条件显示不同内容的能力,可以使用 JavaScript 的条件语法(如 if 语句、&&、三元运算符等)来实现。这是 React 的基本特性之一,不需要第三方库,也不限于类组件。
知识点: if 语句条件渲染 题目: 在 React 组件中使用 if 语句进行条件渲染时,以下哪种做法是正确的? 选项: A. 在 JSX 中直接使用 if 语句✅ B. 在 render 方法或函数组件中使用 if 语句,然后返回不同的 JSX✅ C. 只能在 componentDidMount 生命周期中使用 if 语句✅ D. if 语句不能用于 React 条件渲染✅
解析: 在 React 中,可以在 render 方法或函数组件中使用 if 语句来决定返回哪种 JSX 结构。这种方法的优点是逻辑清晰,适合处理复杂的条件分支。JSX 中不能直接使用 if 语句,需要在外部逻辑中使用。
知识点: 返回 null 跳过渲染 题目: 在 React 组件中,如果不想渲染任何内容,可以怎么做? 选项: A. 返回 undefined✅ B. 返回 false✅ C. 返回 null✅ D. 不写 return 语句✅
正确答案: C
原文依据: 「In some situations, you won’t want to render anything at all. For example, say you don’t want to show packed items at all. A component must return something. In this case, you can return null.」(出自:React 官方文档,第3页)
解析: 三元运算符的主要优势是可以直接嵌入到 JSX 中,使代码更简洁。它适合处理简单的条件判断,但对于复杂的条件逻辑,可能会降低代码可读性。三元运算符在函数组件和类组件中都可以使用,执行速度与 if 语句相当。
知识点: 逻辑与(&&)运算符条件渲染 题目: 在 React 中使用 && 运算符进行条件渲染时,以下哪种情况会导致问题? 选项: A. 条件为 true 时✅ B. 条件为 false 时✅ C. 条件为数字 0 时✅ D. 条件为空字符串时✅
正确答案: C
原文依据: 「Pitfall: Don’t put numbers on the left side of &&. To test the condition, JavaScript converts the left side to a boolean automatically. However, if the left side is 0, then the whole expression gets that value (0), and React will happily render 0 rather than nothing.」(出自:React 官方文档,第5页)
知识点: 条件渲染变量赋值 题目: 在 React 中,使用变量存储条件渲染结果的优势是什么? 选项: A. 提高渲染性能✅ B. 减少内存使用✅ C. 代码更灵活,可读性更好✅ D. 自动优化 DOM 操作✅
正确答案: C
原文依据: 「When the shortcuts get in the way of writing plain code, try using an if statement and a variable. You can reassign variables defined with let, so start by providing the default content you want to display, the name.」(出自:React 官方文档,第6页)
解析: 使用变量存储条件渲染的结果可以使代码更灵活、更易读,特别是当条件逻辑复杂时。这种方法虽然代码量可能增加,但逻辑更清晰,便于维护。它不会直接影响渲染性能或内存使用,也不会自动优化 DOM 操作。
知识点: switch 语句条件渲染 题目: 在 React 中,switch 语句最适合用于哪种条件渲染场景? 选项: A. 只有两个可能的渲染结果✅ B. 有多个互斥条件的渲染逻辑✅ C. 需要嵌套条件的复杂场景✅ D. 只在类组件中使用的场景✅
知识点: 条件渲染的 falsy 值 题目: 在 JavaScript 中,以下哪个值在条件语句中会被视为 falsy? 选项: A. 数字 1✅ B. 字符串 “false”✅ C. 空数组 []✅ D. null✅
正确答案: D
原文依据: 「The following values evaluate to false (also known as Falsy values): false, undefined, null, 0, NaN, the empty string (“”)」(出自:MDN Web Docs,第3页)
知识点: 三元运算符嵌套 题目: 关于 React 中嵌套使用三元运算符,以下说法正确的是? 选项: A. 嵌套三元运算符总是提高代码可读性✅ B. 嵌套三元运算符可能降低代码可读性✅ C. React 不允许嵌套使用三元运算符✅ D. 嵌套三元运算符比 if 语句执行更快✅
正确答案: B
原文依据: 「This style works well for simple conditions, but use it in moderation. If your components get messy with too much nested conditional markup, consider extracting child components to clean things up.」(出自:React 官方文档,第4页)
解析: 嵌套使用三元运算符可能会降低代码的可读性,特别是当嵌套层级较多时。React 官方建议适度使用,当条件逻辑变得复杂时,考虑提取子组件或使用其他方法(如 if 语句)来保持代码清晰。
知识点: 条件渲染与组件提取 题目: 当条件渲染逻辑变得复杂时,React 推荐的做法是什么? 选项: A. 总是使用 switch 语句✅ B. 使用更多的三元运算符✅ C. 提取子组件来简化逻辑✅ D. 避免使用条件渲染✅
正确答案: C
原文依据: 「If your components get messy with too much nested conditional markup, consider extracting child components to clean things up. In React, markup is a part of your code, so you can use tools like variables and functions to tidy up complex expressions.」(出自:React 官方文档,第4页)
知识点: 条件渲染与 JSX 题目: 在 React 中,JSX 与条件渲染的关系是什么? 选项: A. JSX 不支持任何形式的条件逻辑✅ B. JSX 只支持三元运算符✅ C. JSX 可以通过花括号 {} 嵌入 JavaScript 表达式,包括条件逻辑✅ D. JSX 必须使用特殊的条件标签✅
正确答案: C
原文依据: 「Curly braces open the “window into JavaScript”. Embed the variable with curly braces in the returned JSX tree, nesting the previously calculated expression inside of JSX.」(出自:React 官方文档,第6页)
知识点: 逻辑与(&&)运算符的工作原理 题目: 在 JavaScript 中,表达式 A && B 的求值规则是什么? 选项: A. 如果 A 为 true,则返回 A,否则返回 B✅ B. 如果 A 为 true,则返回 B,否则返回 A✅ C. 总是返回 A 和 B 的布尔值与运算结果✅ D. 总是返回 true 或 false✅
正确答案: B
原文依据: 「A JavaScript && expression returns the value of its right side (in our case, the checkmark) if the left side (our condition) is true. But if the condition is false, the whole expression becomes false.」(出自:React 官方文档,第5页)
解析: 在 JavaScript 中,A && B 表达式的求值规则是:如果 A 为 truthy,则返回 B 的值;如果 A 为 falsy,则返回 A 的值。这种短路求值机制是 && 运算符在 React 条件渲染中发挥作用的基础。
知识点: 条件渲染与性能 题目: 关于条件渲染对 React 性能的影响,以下说法正确的是? 选项: A. 条件渲染总是会降低应用性能✅ B. 条件渲染可以通过减少不必要的渲染来提高性能✅ C. 条件渲染对性能没有任何影响✅ D. 只有使用 && 运算符的条件渲染才能提高性能✅
正确答案: B
原文依据: 「If no matching label is found, the program looks for the optional default clause.」(出自:MDN Web Docs,第2页)
解析: 条件渲染可以通过减少不必要的渲染来提高应用性能。通过只渲染当前需要的组件,可以减少 DOM 操作和计算量,从而提高应用响应速度。不同的条件渲染方法(if、三元、&&、switch)在性能上差异不大,选择应基于代码可读性和维护性。
知识点: 条件渲染与错误处理 题目: 在 React 中,如何处理条件渲染过程中可能出现的错误? 选项: A. 使用 try…catch 语句包裹条件渲染代码✅ B. 使用 Error Boundaries 组件捕获渲染错误✅ C. React 会自动处理所有条件渲染错误✅ D. 条件渲染不会产生错误✅
正确答案: B
原文依据: 「You can throw exceptions using the throw statement and handle them using the try…catch statements.」(出自:MDN Web Docs,第3页)
知识点: 动态类名条件渲染 题目: 在 React 中,如何基于条件动态设置元素的 className? 选项: A. 只能使用 if 语句预先计算类名✅ B. 可以使用三元运算符直接在 className 属性中设置✅ C. 必须使用专门的 CSS-in-JS 库✅ D. React 不支持动态类名✅
知识点: 条件渲染与 React Hooks 题目: 在使用 React Hooks 的函数组件中,条件渲染有什么特殊注意事项? 选项: A. 函数组件不支持条件渲染✅ B. Hooks 必须在组件顶层调用,不能在条件语句中调用✅ C. 条件渲染只能使用 useEffect Hook✅ D. 必须使用 useCondition Hook(一个虚构的 Hook)✅
正确答案: B
原文依据: 「JavaScript supports a compact set of statements, specifically control flow statements, that you can use to incorporate a great deal of interactivity in your application.」(出自:MDN Web Docs,第1页)
学习目标
通过精心设计的选择题和原文对照,帮助学习者掌握核心知识点
使用说明
请仔细阅读每个问题,对照原文理解解析
题目与解析