Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in useSize: Cannot read property 'contentWindow' of null #442

Closed
ManojBahuguna opened this issue Jul 4, 2019 · 0 comments
Closed

Comments

@ManojBahuguna
Copy link
Contributor

Problem

When we redirect in a component using useSize in the first render, we get this error:
Cannot read property 'contentWindow' of null

Reproduction link

https://codesandbox.io/s/react-use-usesize-error-xo3q4

Cause

This seems to be causing the issue:

const iframe: HTMLIFrameElement = ref.current!;
if (iframe.contentWindow) {
window = iframe.contentWindow!;
onWindow(window);

Possible solution

We can check if iframe is defined before accessing it's property

...
useEffect(() => {
    const iframe: HTMLIFrameElement = ref.current!;
    
   if(!iframe) {
    // this will happen if component is already unmounted
    return;
   }

    if (iframe.contentWindow) {
...
streamich pushed a commit that referenced this issue Jul 8, 2019
## [9.8.2](v9.8.1...v9.8.2) (2019-07-08)

### Bug Fixes

* **useSize:** prevents accessing iframe's property when it's not defined ([c9b5cdc](c9b5cdc)), closes [#442](#442)
* **useSize:** prevents accessing iframe's property when it's not… ([#443](#443)) ([8f04e8f](8f04e8f))
* iframe can be null ([a9e3bab](a9e3bab))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant