@samitouri / QOS-React / commits / c35a1e7483

Fix crash during server render in react 16.4.1. (#13088)

* Fix crash during server render. setTimeout and clearTimeout may not be available in some server-render environments (such as ChakraCore in React.NET), and loading ReactScheduler.js will cause a crash unless the existence of the variables are checked via a typeof comparison. https://github.com/reactjs/React.NET/issues/555 The crash did not occur in 16.4.0, and the change appears to have been introduced here: https://github.com/facebook/react/pull/12931/files#diff-bbebc3357e1fb99ab13ad796e04b69a6L47 I tested this by using yarn link and running it with a local copy of React.NET. I am unsure the best way to unit test this change, since assigning null to `setTimeout` causes an immediate crash within the Node REPL. * Fix flow errors and log warning if setTimeout / clearTimeout are not defined / not a function. * Use invariant to assert setTimeout / clearTimeout are functions * Remove use of invariant * Explain

Dustin Masters committed Jun 22, 2018 at 12:07 UTC c35a1e74835d2b4c07f1e14779a22ae8ca5a0d10
1 file changed +9 -2
packages/react-scheduler/src/ReactScheduler.js
+9 -2
@@ -60,8 +60,15 @@ if (__DEV__) {
60 // this module is initially evaluated.
61 // We want to be using a consistent implementation.
62 const localDate = Date;
63 -const localSetTimeout = setTimeout;
64 -const localClearTimeout = clearTimeout;
63 +
64 +// This initialization code may run even on server environments
65 +// if a component just imports ReactDOM (e.g. for findDOMNode).
66 +// Some environments might not have setTimeout or clearTimeout.
67 +// https://github.com/facebook/react/pull/13088
68 +const localSetTimeout =
69 + typeof setTimeout === 'function' ? setTimeout : (undefined: any);
70 +const localClearTimeout =
71 + typeof clearTimeout === 'function' ? clearTimeout : (undefined: any);
72
73 const hasNativePerformanceNow =
74 typeof performance === 'object' && typeof performance.now === 'function';