我试图将套接字功能添加到我拥有的组件中。问题是,当我收到一条消息时,我无法访问组件道具,以便调用我需要调用的actionCreators。
class Navbar extends React.Component<NavbarType> {
const {updateSyncSubmissions} = this.props.updateSyncSubmissions;
componentWillMount() {
let socket = new Socket ( 'ws:localhost:4000/socket' ,
{ transport: websocket.w3cwebsocket } );
socket.connect();
socket.onOpen(function(){
console.log("connection open");
});
let channelName = "notification_room:"+this.props.userId;
let channel = socket.channel(channelName,{});
channel.join()
.receive("ok", function (resp) {
console.log("CONNECTED TO CHANNEL ");
console.log(resp);
});
channel.on("notification:msg", function(resp) {
console.log("RECIEVED");
console.log(resp);
});
channel.on("notification:sync", (resp) =>{
console.log("SYNCCCCCC", this);
updateSyncSubmissions(resp.msj.percentage, resp.msj.is_done);
console.log(resp);
});
}
}
在上面的代码中,我无法访问updateSyncSubmissions
功能内的on("notification:sync")
,因为&#39;这个&#39;它在另一个范围内。
当我通过组件上的套接字收到新消息时,有什么方法可以调度我的操作吗?