React: Material UI 无法显示消息(Snackbar)

huangapple go评论77阅读模式
英文:

React : Material UI cannot display message (Snackbar)

问题

我创建了一个自定义的警告框如下所示,但当我添加 Snackbar 时,它不显示。这是我更改的部分,其余部分相同:

***AlertPopup:***

```jsx
const AlertPopup = () => {
  const { text, type } = useAlert();

  if (text && type) {
    return (

      // 当我使用这个时,消息会显示
      // <Alert variant="filled" severity={type} sx={{}}>
      //   {text}
      // </Alert>

      // 当我使用这个时,消息不会显示
      <Stack spacing={2} sx={{ width: '100%' }}>
      <Snackbar autoHideDuration={6000}>
      <Alert variant="filled" severity={type} >
        {text}
      </Alert>
      </Snackbar>
      </Stack>

    );
  } else {
    return <></>;
  }
};

这是实现的其他部分。我没有更改其他部分(AlertContextuseAlert 类),因为没有特定的 Alert 组件:

那么,这个实现缺少什么?


<details>
<summary>英文:</summary>

I created a custom Alert as shown below, but when I add Snackbar, it is not displaying. Here is the part that I changed, the rest is the same:


***AlertPopup:***

const AlertPopup = () => {
const { text, type } = useAlert();

if (text && type) {
return (

  // when I use this, the message is displayed
  // &lt;Alert variant=&quot;filled&quot; severity={type} sx={{}}&gt;
  //   {text}
  // &lt;/Alert&gt;

  // when I use this, the message is NOT displayed
  &lt;Stack spacing={2} sx={{ width: &#39;100%&#39; }}&gt;
  &lt;Snackbar autoHideDuration={6000}&gt;
  &lt;Alert variant=&quot;filled&quot; severity={type} &gt;
    {text}
  &lt;/Alert&gt;
  &lt;/Snackbar&gt;
  &lt;/Stack&gt;

);

} else {
return <></>;
}
};


Here is the other parts of the implementation. I did not changed other parts (`AlertContext` and `useAlert` classes) as there is not specific component for Alert:



So, what is missing with this implementation? 




</details>


# 答案1
**得分**: 1

尽量将其包装成一个单独的JSX元素。

<details>
<summary>英文:</summary>

It may be silly but try to wrap it into a single JSX element

</details>



# 答案2
**得分**: 1

[Material UI Snackbar](https://mui.com/material-ui/api/snackbar/) 的可见性由 **open** 属性控制。通常情况下,您组件状态的某种改变会触发 Snackbar 的显示。请参考[文档中的基本示例](https://mui.com/material-ui/react-snackbar/#simple-snackbars)。

您的代码可以更新如下:

```javascript
const AlertPopup = () => {
    const [open, setOpen] = React.useState(false);
    const { text, type } = useAlert();

    React.useEffect(() => {
        // 测试触发 Snackbar 显示的某些条件。
        if (<Snackbar 应该显示的条件>) {
            setOpen(true);
        }

        // 依赖数组:请参考 https://reactjs.org/docs/hooks-effect.html#tip-optimizing-performance-by-skipping-effects
    }, [<依赖数组:包括触发条件中使用的值>] );

    if (text && type) {
        return (
            // 当我使用这个时,消息会显示
            // <Alert variant="filled" severity={type} sx={{}}>
            //     {text}
            // </Alert>

            // 当我使用这个时,消息不会显示
            <Stack spacing={2} sx={{ width: '100%' }}>
                <Snackbar open={open} autoHideDuration={6000}>
                    <Alert variant="filled" severity={type}>
                        {text}
                    </Alert>
                </Snackbar>
            </Stack>
        );
    } else {
        return <></>;
    }
};

(Note: I have retained the code part in its original language as you requested.)

英文:

The visibility of the Material UI Snackbar is controlled by the open property. Typically, some change in the state of your component would trigger the display of the Snackbar. See the basic example from the docs.

Your code could be updated like this:

const AlertPopup = () =&gt; {
    const [ open, setOpen ] = React.useState( false );
    const { text, type } = useAlert();

    React.useEffect( () =&gt; {
        // Test for some condition that triggers the display of the Snackbar.
        if ( &lt;Snackbar should be displayed&gt; ) {
            setOpen( true );
        }
        
        // Dependencies array: see https://reactjs.org/docs/hooks-effect.html#tip-optimizing-performance-by-skipping-effects
    }, [ &lt;Dependencies array: includes value(s) used by the effect for the test condition&gt; ] ); 

    if (text &amp;&amp; type) {
        return (

            // when I use this, the message is displayed
            // &lt;Alert variant=&quot;filled&quot; severity={type} sx={{}}&gt;
            //     {text}
            // &lt;/Alert&gt;

            // when I use this, the message is NOT displayed
            &lt;Stack spacing={2} sx={{ width: &#39;100%&#39; }}&gt;
            &lt;Snackbar open={open} autoHideDuration={6000}&gt;
            &lt;Alert variant=&quot;filled&quot; severity={type} &gt;
                {text}
            &lt;/Alert&gt;
            &lt;/Snackbar&gt;
            &lt;/Stack&gt;

        );
    } else {
        return &lt;&gt;&lt;/&gt;;
    }
};

huangapple
  • 本文由 发表于 2023年3月12日 15:28:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711650.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定