执行套接字控制器功能时尝试更改屏幕区域中的状态

时间:2020-10-07 23:06:11

标签: javascript reactjs react-native

我有一个用于发送和接收消息的套接字实例类。该类工作正常,我相应地接收和发送消息。我的问题是我需要从屏幕上更改一个称为套接字实例类函数的状态。

export default class SocketInstance
{
    ...
    listenEvents = (myUsername,reRenderState) =>
    {
        if(SocketInstance.connection !== null)
        {
            SocketInstance.connection.on(myUsername,(notificationObj)=>{
                if(notificationObj.type == 'hostingNotification')
                {   
                    console.warn(reRenderState)
                    return !reRenderState
                    
                }
            } 
        }
    }
}

到目前为止,SocketInstance类中的console.warn语句将执行,一切正常。我以为,我可以返回在return语句中传递给listenEvents函数的相反状态,并将其带到另一个类中。

export default class AA extends Component
{

    async componentDidMount()
    { 
        this.socket = SocketInstance.getSocketInstance();
        const reRenderFrinds = this.socket.listenEvents(username,this.state.renderFriendList)
        console.warn(reRenderFrinds)
    }
}

但是,AA类中的console.warn语句不执行。这意味着我无法继续使用该功能,也无法在AA类中更改任何状态。我也尝试了以下操作,但是console.warn无法执行,这意味着我将无法更改任何状态。

export default class AA extends Component
{
    
    async componentDidMount()
    { 
        this.socket = SocketInstance.getSocketInstance();
        this.socket.listenEvents(username,this.state.renderFriendList,(res)=>{
            console.warn(res)
        }
    }
}

有人可以告诉我我在做什么错。预先谢谢你。

0 个答案:

没有答案