Q:

Assigning value to global variable in ReactJs ES5

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.

A:

You can use localstorage for this.

//Set your gloabal variable from any where in your app by
localStorage.setItem("key(variable-name)","value")

//Get your gloabal variable from any where in your app by
localStorage.getItem("key(variable-name)")

