Monkeypatch Math.random(), new Date() and Date.now() during render() in dev
See original GitHub issueWas talking with Adam Wolff today about rxjs and there are a ton of common bugs with these functions. You’re gonna have a bad time if you use these in React render()
methods too – if we monkeypatched these to warn in __DEV__
we could prevent some bugs. @jordwalke seemed to like the idea too.
Issue Analytics
- State:
- Created 10 years ago
- Comments:20 (19 by maintainers)
Top Results From Across the Web
Monkey patch JavaScript new Date() - Stack Overflow
The only thing I need to change is new Date() , which I need to always return the same date. There is a...
Read more >3 ways to monkey-patch without making a mess - Justin Weiss
Monkey Patching. When you first try Ruby, it's amazing. You can add methods right to core classes! You don't have to call Time.now....
Read more >So, apparently there's now code in React to monkeypatch ...
seems to be preparation for the use RFC, where some requests are cached for rendering. providing a custom AbortSignal to your request will ......
Read more >Generate random date with JavaScript - JavaScriptF1.com
Generating a random date can be implemented by generating a random timestamp and using it in the Date constructor.
Read more >News — JRuby.org
A workaround is provided, via patching the pure-Ruby date code in your own JRuby ... #6440 - Java::JavaLang::NullPointerException () --> JRuby 9.2.13.0 on...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@steveluscher That sounds kind of fragile to me, I think time is ubiquitous enough that
Date.now()
should be exported by React asReact.now()
. So that when any rendering inside React takes place, time is frozen and all subsequent calls toReact.now()
returns that same time.For
React.renderComponentToString
the frozen time would also be added to the root element next to the checksum, which would be used for the initial rendering on the client when reusing the DOM.Aside from just fixing mismatches between client and server rendering, it would also prevent edge-case inconsistencies in rendering. Two identical timer components can currently show slightly varying times, or say an item could show as expired in one place and not expired in another if the timing is just right. This would prevent that entirely.
@petehunt Any spontaneous reaction?
This has been inactive for over a year, and it doesn’t appear like there any plans to do this. With the explosion of JavaScript build tools and polyfills, we have been trying to be careful to not add surprises or touch the built-ins, and I think it has been the right call. React Native is actually running into some issues right now because of conflicting polyfills, and it’s great we’ve been able to avoid those so far by not touching the built-ins.
I’m closing this but please let me know if I’m making a mistake and we should reopen. Thanks!