简体   繁体   English

在反应 js 中显示随机数 enqueuesnackbar (notistack)

[英]Shows random number enqueuesnackbar (notistack) in react js

I am using notistack package.我正在使用 notistack package。 need to show snackbar on the screen.需要在屏幕上显示小吃吧。 while calling the Snack component which is using enquesnackbar it shows the snackbar but also show the random number on the screen.在调用使用 enquesnackbar 的 Snack 组件时,它会显示小吃栏,但也会在屏幕上显示随机数。

I want to remove that random number from screen.我想从屏幕上删除那个随机数。 it should not visible on the screen.它不应该在屏幕上可见。

code sandbox demo代码沙盒演示

https://codesandbox.io/s/naughty-microservice-ocoig2?file=/src/index.js https://codesandbox.io/s/naughty-microservice-ocoig2?file=/src/index.js

I got the temporary solution for that.我得到了临时解决方案。

I have added display non in the component.我在组件中添加了非显示。 In this case random number will be there but it won't be visible on screen.在这种情况下,随机数将存在,但不会在屏幕上显示。

  <div style={{display: "none"}}>
    {messageList.map(({ message }) => (
      <Snack message={message} />
    ))}
    </div>

if anyone found any other solution feel free to post here.如果有人找到任何其他解决方案,请随时在此处发布。

I experienced this issue too.我也遇到过这个问题。 It was throwing random numbers.它正在抛出随机数。 I moved my enquesnackbar out of any HTML element and placed it in a function and called it whenever required by passing down the message values and variant.我将 enquesnackbar 从任何 HTML 元素中移出,并将其放在 function 中,并在需要时通过传递消息值和变体来调用它。 Note: I'm using their hooks.注意:我正在使用他们的钩子。

import { useSnackbar } from "notistack";

const { enqueueSnackbar, closeSnackbar } = useSnackbar();

const showAlert = (err, variant) => { enqueueSnackbar(err, { variant: variant, preventDuplicate: true, }); };

then in template然后在模板中

{error? showAlert(error, "error"): null}

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

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