I'm new to Reactjs and I'm developing a simple application using React and SocketIO. I'm trying to declare a global variable and assign {data.name} value to it (code shown below).
socket.on('loginSuccess', function (data) {
if (data.type == 'teacher') {
ReactDOM.render(
<div><TeacherLayout userName={data.name}/></div>,
document.getElementById('root')
);
} else {
ReactDOM.render(
<div><StudentLayout userName={data.name}/></div>,
document.getElementById('root')
);
}
reactCookie.save('sid', data.id);
});
I want to pass the global variable to userName of the code shown below,
socket.on('sessionstart', function (data) {
ReactDOM.render(
<div><MainLayout userName={globalVariable}/></div>,
document.getElementById('root')
);
}
reactCookie.save('sid', data.id);
});
I would really appreciate some help. Thanks.