How to resolve Warning: known event handler property `onExited`. It will be ignored in StatusSnackbar component

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

How to resolve Warning: known event handler property `onExited`. It will be ignored in StatusSnackbar component

问题

以下是代码部分的中文翻译:

 StatusSnackbar 组件中出现警告已知的事件处理程序属性 `onExited`如何解决此问题

**组件**

import Snackbar from '@material-ui/core/Snackbar'
import { withStyles } from '@material-ui/core/styles'
import PropTypes from 'prop-types'
import React, { useCallback, useState } from 'react'
import NotificationType from '../../../../types/notification'
import styles from './styles'

const Component = ({
    classes,
    notification,
    onRemove: removeNotification
}) => {

    const [ open, setOpen ] = useState(true)

    const handleClose = useCallback((e, reason) => {
        if (reason !== 'clickaway') {
            setOpen(false)
        }
    }, [])

    return (
        <Snackbar
            className={classes.root}
            anchorOrigin={{ horizontal: 'right', vertical: 'bottom' }}
            autoHideDuration={notification.duration}
            open={open}
            onClose={handleClose}
            onExited={removeNotification}
            message={notification.message}
            variant={notification.variant}
        />
    )
}

Component.displayName = 'Notifications.Notification'
Component.propTypes = {
    classes: PropTypes.shape({
        root: PropTypes.string.isRequired
    }).isRequired,
    notification: NotificationType.isRequired,
    onRemove: PropTypes.func.isRequired
}

export default withStyles(styles)(Component)

希望这能帮助您理解代码部分的内容。如果您需要进一步的帮助,请随时告诉我。

英文:

In StatusSnackbar component getting Warning: known event handler property onExited, how to resolve the same.

Component:

import Snackbar from &#39;@material-ui/core/Snackbar&#39;
import { withStyles } from &#39;@material-ui/core/styles&#39;
import PropTypes from &#39;prop-types&#39;
import React, { useCallback, useState } from &#39;react&#39;
import NotificationType from &#39;../../../../types/notification&#39;
import styles from &#39;./styles&#39;
const Component = ({
classes,
notification,
onRemove: removeNotification
}) =&gt; {
const [ open, setOpen ] = useState(true)
const handleClose = useCallback((e, reason) =&gt; {
if (reason !== &#39;clickaway&#39;) {
setOpen(false)
}
}, [])
return (
&lt;Snackbar
className={classes.root}
anchorOrigin={{ horizontal: &#39;right&#39;, vertical: &#39;bottom&#39; }}
autoHideDuration={notification.duration}
open={open}
onClose={handleClose}
onExited={removeNotification}
message={notification.message}
variant={notification.variant}
/&gt;
)
}
Component.displayName = &#39;Notifications.Notification&#39;
Component.propTypes = {
classes: PropTypes.shape({
root: PropTypes.string.isRequired
}).isRequired,
notification: NotificationType.isRequired,
onRemove: PropTypes.func.isRequired
}
export default withStyles(styles)(Component)

答案1

得分: 0

I have replaced onExited with onClose in the following way, and it is working as I expected, without any warning.

Component:

import Snackbar from '@material-ui/core/Snackbar'
import { withStyles } from '@material-ui/core/styles'
import PropTypes from 'prop-types'
import React, { useCallback, useState } from 'react'
import NotificationType from '../../../../types/notification'
import styles from './styles'

const Component = ({
    classes,
    notification,
    onRemove: removeNotification
}) => {

    const [ open, setOpen ] = useState(true)

    const handleClose = useCallback((e, reason) => {
        if (reason !== 'clickaway') {
            setOpen(false)
        }
    }, [])

    return (
        <Snackbar
            className={classes.root}
            anchorOrigin={{ horizontal: 'right', vertical: 'bottom' }}
            autoHideDuration={notification.duration}
            open={open}
            onClose={removeNotification}
            onClick={handleClose}
            message={notification.message}
            variant={notification.variant}
        />
    )
}

Component.displayName = 'Notifications.Notification'
Component.propTypes = {
    classes: PropTypes.shape({
        root: PropTypes.string.isRequired
    }).isRequired,
    notification: NotificationType.isRequired,
    onRemove: PropTypes.func.isRequired
}

export default withStyles(styles)(Component)

(Note: I've translated the code comments only and not the code itself.)

英文:

I have replaced onExited by onClose in following way and it working as I expected way without any warning.

Component:

import Snackbar from &#39;@material-ui/core/Snackbar&#39;
import { withStyles } from &#39;@material-ui/core/styles&#39;
import PropTypes from &#39;prop-types&#39;
import React, { useCallback, useState } from &#39;react&#39;
import NotificationType from &#39;../../../../types/notification&#39;
import styles from &#39;./styles&#39;
const Component = ({
classes,
notification,
onRemove: removeNotification
}) =&gt; {
const [ open, setOpen ] = useState(true)
const handleClose = useCallback((e, reason) =&gt; {
if (reason !== &#39;clickaway&#39;) {
setOpen(false)
}
}, [])
return (
&lt;Snackbar
className={classes.root}
anchorOrigin={{ horizontal: &#39;right&#39;, vertical: &#39;bottom&#39; }}
autoHideDuration={notification.duration}
open={open}
onClose={removeNotification}
onClick={handleClose}
message={notification.message}
variant={notification.variant}
/&gt;
)
}
Component.displayName = &#39;Notifications.Notification&#39;
Component.propTypes = {
classes: PropTypes.shape({
root: PropTypes.string.isRequired
}).isRequired,
notification: NotificationType.isRequired,
onRemove: PropTypes.func.isRequired
}
export default withStyles(styles)(Component)

huangapple
  • 本文由 发表于 2023年5月7日 22:16:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194473.html
匿名

发表评论

匿名网友

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

确定