简体   繁体   English

open={Boolean(anchor)} 对 MUI 组件意味着什么

[英]What does open={Boolean(anchor)} mean for a MUI component

Working with some legacy code where a MUI menu has been defined within a React component written with TypeScript as below:使用一些遗留代码,其中 MUI 菜单已在使用 TypeScript 编写的 React 组件中定义,如下所示:

interface Props {
anchor: HTMLButtonElement | null;
}
...
<Menu
  id="order-menu"
  anchorEl={anchor}
  open={Boolean(anchor)}
  onClose={onClose}
>
...
</Menu>

Not sure if I understand the meaning or purpose of the syntax open={Boolean(anchor)} .不确定我是否理解语法open={Boolean(anchor)}的含义或目的。 It would be kind if anyone can explain a bit.如果有人能解释一下,那就太好了。

Boolean(...) returns a boolean value, ie true or false . Boolean(...)返回 boolean 值,即truefalse

If anchor is truthy (ie there's an object), then Boolean(anchor) returns true .如果anchor是真实的(即有一个对象),则Boolean(anchor)返回true

If anchor is falsy (eg it's null ), then Boolean(anchor) returns false .如果anchor是假的(例如它是null ),则Boolean(anchor)返回false

In your code snippet, it means that Menu prop open will be set to true if anchor is an object, and false if there's no object (if anchor is null ).在您的代码片段中,这意味着如果anchor为 object,则 Menu 属性open将设置为true ,如果没有 object(如果anchornull ),则设置为false

Syntax open={Boolean(anchor)} refers to whether your Menu will open/close as the type of open props is Boolean. And here Boolean(anchor) type converting is used to convert anchor value in to Boolean value.语法 open={Boolean(anchor)} 指的是你的 Menu 是否打开/关闭,因为open props 的类型是 Boolean。这里的Boolean(anchor)类型转换用于将 anchor 值转换为 Boolean 值。

So when you set anchor value as like event.target.value or ref based then Boolean(anchor) === true But when you reset the anchor value like null or undefined then Boolean(anchor) === false因此,当您将值设置为 event.target.value 或 ref based then Boolean(anchor) === true 但是当您将锚值重置为 null 或 undefined 然后 Boolean(anchor) === false

So based on that your Menu Element open/close因此,基于您的菜单元素打开/关闭

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM