question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Accessing window properties inside useEffect leads to "Invalid calling object" error in IE 11

See original GitHub issue

Hello,

I’ve noticed in two specific use cases that accessing window properties inside a useEffect leads to the error “Invalid calling object” in Internet Explorer 11.

I have not been able to isolate a minimum demo of the problem unfortunately.

First problem - window scroll useEffect(() => { window.scroll(0, 0); }, [])

Second problem - Apollo client polling useEffect(() => { startPolling(120000); return stopPolling; }, []); https://github.com/apollographql/apollo-client/blob/6f579ea2cdf8c78acb004a4457a7a6764d64091e/packages/apollo-client/src/scheduler/scheduler.ts The start polling method internally uses setInterval.

In both cases, replacing the useEffect by lifecycles removes the error.

React version: 16.7.0-alpha.2

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
hipstersmoothiecommented, Aug 14, 2019

We are seeing the same issue. currently we will not be able to get a repro for a few weeks.

But i can confirm that the bug happens under these conditions:

  1. Have a custom hook that uses window properties
import React from 'react';

/**
 * Determine if the user has "reduce-motion" enabled in their browser.
 *
 * @example
 * const isReducedMotion = useReduceMotion();
 */
export const useReduceMotion = () => {
  const [reduceMotion, setReduceMotion] = React.useState(
    window.matchMedia('(prefers-reduced-motion: reduce)').matches
  );

  // With either useEffect or useLayoutEffect this breaks. I can even comment
  // out the entire inside of the effect and the error happens.
  React.useEffect(() => {
    const mediaQuery = window.matchMedia('(prefers-reduced-motion: reduce)');

    /** Ran when the user changes this setting. */
    const changeReduceMotion = () => setReduceMotion(!reduceMotion);

    mediaQuery.addListener(changeReduceMotion);

    return () => {
      mediaQuery.removeListener(changeReduceMotion);
    };
  }, [reduceMotion]);

  return reduceMotion;
};
  1. Have devtool: 'eval-source-map' somewhere in a webpack build

  2. Open IE

Screen Shot 2019-08-14 at 3 03 35 PM

1reaction
gaearoncommented, Nov 21, 2018

Fair enough 🙂 If you do find a reduced case we’d appreciate sharing it.

All useEffect does is run the work a bit later, when the browser isn’t busy. It’s hard to guess which APIs don’t work in this case in IE or why.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Invalid hook call. Hooks can only be called inside of the body ...
I want to show some records in a table using React but I got this error: Invalid hook call ...
Read more >
Invalid Hook Call Warning - React
You are probably here because you got the following error message: Hooks can only be called inside the body of a function component....
Read more >
A Definitive Guide to Handling Errors in JavaScript - Kinsta
This error occurs when you try to invoke a method that doesn't exist in your script, or it does but can not be...
Read more >
CHANGELOG.md - facebook/react - Sourcegraph
renderToString : Will no longer error when suspending on the server. Instead, it will emit the fallback HTML for the closest <Suspense> boundary...
Read more >
TypeScript errors and how to fix them
Alternatively, you can write your function implementation inside a source code file ( *.ts ). TS1192. error TS1192: Module ' json5 ' has...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found