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:
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.
Here is the error: TypeError: Cannot read property "getItem" from undefined
. Odd enough, it works in componentDidMount .
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.
Found one or two other questions on similar topics but nothing to help explain why getItem
is not available on sessionStorage
.
您可以在ComponentDidMount
使用sessionStorage
。
Add this method to your component class(.ts file).
get sessionStorage() {
return sessionStorage;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.