【问题标题】:Set timeout for message设置消息超时
【发布时间】:2021-11-10 14:46:57
【问题描述】:

我使用mapStateToProps显示通知消息:

function TabPanel(props) {

    {props.webSocketMessage ? (<Typography className={classes.invalidLabel} color="textSecondary">Successful { props.webSocketMessage}</Typography>) : "" }


    const mapStateToProps = state => ({
        webSocketMessage: state.webSocketMessage
    });
}

代码有效,但我想在 30 秒后隐藏消息。

如何实现这个功能?

【问题讨论】:

    标签: reactjs redux react-redux


    【解决方案1】:

    通常,(不是使用 Redux)它会在任何父组件中处理,该组件将 webSocketMessage 作为 prop 传递给该组件。假设您将消息作为状态存储在父级中,它可能类似于...

    import React, {useEffect, useState} from 'react';
    
    const ParentComponent = () => {
      const [webSocketMessage, setWebSocketMessage] = useState()
      useEffect(() => {
        if (webSocketMessage){
          setTimeout(() => setWebsocketMessage(null), 30000)
        }
      }, [webSocketMessage])
    
      return <ChildComponent webSocketMessage={webSocketMessage} />
    }
    
    export default ParentComponent;
    

    同样的想法可以转化为清除消息的 redux 操作,因为您正在使用 redux 存储来保存该消息。基本思想是负责发送该道具的任何人(在您的情况下为 Redux)也需要负责清除它,因此您最终得到...

    const clearMessage = () => dispatch({type: 'CLEAR_MESSAGE'})
    

    dispatch({type: 'CLEAR_MESSAGE'}) 然后可以在你的 reducer 中用于将状态中的消息设置为 null、一个空数组或任何你需要的东西。

    您的消息缩减器可能如下所示:

    export function messageReducer(state = [], action) {
      switch(action.type){
        case 'ADD_MESSAGE':
          return [...state, action.errorMessage]
        case 'CLEAR_MESSAGES':
          return []
        default:
          return state
      }
    }
    

    只要你的根 reducer 已经使用了某种消息 reducer,你就应该能够将这种情况添加到 switch 语句中。

    最后,在这个 sn-p 取自的组件中,你可以有类似...

    useEffect(() => {
      setTimeout(props.clearMessages, 30000)
    }
    

    最终

    const Whatever = () => {
      // code
      useEffect(() => {
        setTimeout(props.clearMessages, 30000)
      }
      //other code
    }
    
    const mapStateToProps = (state) => {
      return {
        webSocketMessage: state.webSocketMessage
      }
    }
    
    const mapDispatchToProps = (dispatch) => {
      return {
        clearMessages: () => dispatch({type: 'CLEAR_MESSAGES'})
      }
    }
    
    export default connect(mapStateToProps, mapDispatchToProps)(Whatever);
    

    【讨论】:

    • 我试过这个:useEffect(() =&gt; { if (props.webSocketMessage){ setTimeout(() =&gt; props.webSocketMessage.clear(), 5000) } }, [props.webSocketMessage]) 但得到TypeError: props.webSocketMessage.clear is not a function 有没有类似的解决方案?
    • 你需要自己实现一个函数来清除redux store中的数据。绕过你可以做setTimeout(() =&gt; dispatch({type: 'CLEAR_MESSAGE'}), 5000),并确保你添加CLEAR_MESSAGE到你的reducer,这样你就可以清除redux中的消息。您还需要确保您可以访问那里的dispatch()。我建议像上面那样写一个clearMessage 动作,用mapDispatchToProps 将它映射到你的组件道具,然后用props.clearMessage() 调用它,就像上面一样
    • 你能给我看一下代码示例吗?
    • 好的,我会在上面编辑。
    • 我尝试了代码,但是当我运行代码时,props 的其他值未定义。有没有其他方法可以实现这个功能?
    猜你喜欢
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多