Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

requestAnimationFrame is not defined it Next.js with React Native Web (Animated module)

I'm working on Next.js and React-Native-Web. I managed to run them together following the official Next.js example but when I'm trying to use the Animated package from the react-native it fails with Error that the requestAnimationFrame isn't defined. Basically this functionality does the node_modules package but I set the alias in webpack to translate all react-native requires to the react-native-web so even the node_modules package should use the react-native-web.

Any suggestions on how to solve it?

ReferenceError: requestAnimationFrame is not defined
    at start (...node_modules\react-native-web\
dist\cjs\vendor\react-native\Animated\animations\TimingAnimation.js:104:11)
enter code here

Screenshot of the Error

Thanks for any help!

like image 807
Velidan Avatar asked Apr 25 '20 16:04

Velidan


1 Answers

The problem is in the missed RequestAnimationFrame functionality at the server. This error happens when Next.js tries to render the component during SSR. Unfortunately, there is no polyfill, etc. for such purpose so I just decided to use the Next.js dynamic imports for a Component that has animation functionality.

Next.js Official documentation

My own case оust to show how code looks:

import dynamic from 'next/dynamic';

const AutocompleteDropdown = dynamic(
  () => import(
    'myAwesomeLib/components/dropdown/autocomplete/AutocompleteDropdown'
  ),
  {
    ssr: false,
  }
);

Now you can use the AutocompleteDropdown as the standard JSX component

like image 56
Velidan Avatar answered Nov 04 '22 08:11

Velidan