Actually I am initializing a variable in componentDidMount and from there printing its value on console. So in the console I am getting the value of variable but when I print the value of variable from render I am getting "undefined".
var data //declaring a global variable
export default class Schemes extends React.Component{
constructor(){
super();
this.response = response
componentDidMount(){
/* Some Computation*/
if(localStorage.getItem('xyz')){
data = response
}
}
render(){
console.log("In render", data);
}
classsnippet, showing all what you are asking.componentDidMountruns after the initial render – therefore accessinglocalStorageoccurs after your render function attempts to log the data fromlocalStorage.