react: useEffect causes 'callback is not a function' exception

Do you want to request a feature or report a bug? Bug

What is the current behavior?

useEffect results in the following exception in the console:

Uncaught TypeError: callback is not a function
    at flushFirstCallback (scheduler.development.js:348)
    at flushWork (scheduler.development.js:441)
    at MessagePort.channel.port1.onmessage (scheduler.development.js:188)

I am unable to isolate the problem enough to create a sandbox that reproduces it. I am posting it here in case there is still some value in it (it seems slightly related to https://github.com/react-navigation/react-navigation/issues/5851, https://github.com/gaearon/overreacted.io/issues/460 and even https://github.com/facebook/react/issues/15194). This is my component:

const RemoteControlledSliderRaw = ({ activeIndex, currentlyDragging, className, children }) => {
	const [forcedX, setForcedX] = React.useState(null);

	React.useEffect(() => {
		if (currentlyDragging) {
			window._animateItem.update = setForcedX;
		} else {
			if (window._animateItem.update) {
				setForcedX(null);
				window._animateItem.update = null;
			}
		}
	}, [currentlyDragging]);

	return (
		<div>
			<SliderCore activeIndex={activeIndex} forcedX={forcedX} className={className}>
				{children}
			</SliderCore>
		</div>
	);
};

What might be interesting is that I set a global object to point to a setState setter. That can be called from the outside. I am not sure exactly how the exception occurs, but if I comment out the entire useEffect bit, it goes away.

What is the expected behavior? No exception. Or at least one that provides some explanation as to what is wrong.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

16.8.6. I don’t know if this worked with earlier versions.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 3
  • Comments: 15

Commits related to this issue

Most upvoted comments

I’ve seen this happen because of wrong scheduler version. Unfortunately RN didn’t have explicit dependency on scheduler version (by mistake). I fixed this on RN master but I don’t think it was released yet.

If you see this, try adding "scheduler": "0.14.0" dependency into your project (or, if that breaks your project, try the opposite — "scheduler": "0.13.6").

Try adding "scheduler": "0.14.0" to your package.json and re-running your package manager.

Not work for me with react-konva. I try both 0.14 and 0.13.6

That’s not a real fix and will likely blow up later. I recommend to fix the underlying problem instead.

I have fixed the problem by using useLayoutEffect instead of useEffect it works fine 😃

Walking around that issue I finded solution:

  1. this problem occurs when there are multiple versions of scheduler in the project,
  2. for check this use npm list scheduler, or yarn why scheduler (more info here)
  3. if you have several versions of scheduler check link above or
  • use with npm this
  • use with yarn like this:
"resolutions": {
    "scheduler": "0.15.0"
  }
  1. in any case after each manipulation with scheduler ALWAYS reload you react app (e.g. npm start)

Thank you @gaearon, the bug has been fixed, I just did npm install scheduler@0.14.0 --save