Cannot getItem from sessionStorage in ComponentWillMount?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Cannot getItem from sessionStorage in ComponentWillMount?



Just looking to pull in some data sitting in sessionStorage inside of componentWillMount but I continue running into: cannot getItem of undefined. Here is what this looks like:


sessionStorage


componentWillMount


cannot getItem of undefined


componentWillMount()
console.log(window.sessionStorage); // logs sessionStorage
console.log(window.sessionStorage.getItem('some-key')); // errors out here



EDIT: Here is a screenshot. The key is different because I was just using something else for the example, but the error is the same.



enter image description here



Here is the error: TypeError: Cannot read property "getItem" from undefined. Odd enough, it works in componentDidMount.


TypeError: Cannot read property "getItem" from undefined



For clarification: I'm using window.sessionStorage instead of sessionStorage because for some reason it can't recognize it without appending it to the window object.


window.sessionStorage


sessionStorage


window



Found one or two other questions on similar topics but nothing to help explain why getItem is not available on sessionStorage.


getItem


sessionStorage





window.sessionStorage is defined?
– taha
Jul 10 '17 at 21:32


window.sessionStorage





@taha Yeah, comes up defined with all the proper key/value pairs.
– Jose
Jul 10 '17 at 21:32





Have you tried dropping the window. part and just using sessionStorage?
– Scott Marcus
Jul 10 '17 at 21:35


window.


sessionStorage





are you stringifying entries before storing them?
– taha
Jul 10 '17 at 21:36





@taha That wouldn't account for this error.
– Scott Marcus
Jul 10 '17 at 21:36




1 Answer
1



You can use sessionStorage in ComponentDidMount.


sessionStorage


ComponentDidMount






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

React Native Navigation and navigating to another Screen problem