-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allows the force update to be infinitely called #2982
Conversation
|
Nice catch! Random idea with integer unsafety, an alternative might be to use |
ea5937f
to
267c3da
Compare
Modulo is expensive from last time I checked (2005'ish 😅 ) and we can't really assume how many renders React could batch, or can we? |
Fair points :)
…On Thu, Jun 10, 2021 at 8:55 PM Olivier Rousseau ***@***.***> wrote:
Nice catch! Random idea with integer unsafety, an alternative might be to
use (tick => ++tick % 1000)?
Modulo is expensive from last time I checked (2005'ish 😅 ) and we can't
really assume how many renders React could batch, or can we?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#2982 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN4NBBLVMOZZSDUIYQT3GTTSEKEXANCNFSM46PC2PVA>
.
|
Also to mention, it's the first time I contribute to this project, I couldn't find the |
I think |
Btw should we worry about
|
I think that would even be better, I'll give it a try when I have a chance |
Hope that they will never use Object.is in that case, as that would make
everything fall apart :')
…On Fri, Jun 11, 2021 at 1:07 PM Olivier Rousseau ***@***.***> wrote:
I think useState uses strict comparison, can't we just setState(NaN)?
I think that would even be better, I'll give it a try when I have a chance
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#2982 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN4NBABMR3IXILLGZQCD6LTSH4AZANCNFSM46PC2PVA>
.
|
So I tried using I think using an object reference would work better since function useForceUpdate() {
const [, setState] = useState({});
return () => setState(() => ({}));
} alternatively, this works too but side-effects, if any, would be triggered in a different order because we don't rely on the function useForceUpdate() {
const [value, setState] = useState(true);
return () => setState(!value);
} |
Spotted on! They do use a copy of the polyfill in their |
Ah it's noted here, could't find it before (it's not mentioned in useState docs...).
👍 (without the extra closure) |
Maybe I missed something, I understand what's being said but it doesn't seems to be the actual behaviour as of react 16.8.0 function useForceUpdateNoValue() {
const [, setState] = useState();
return () => setState();
} it doesn't update, I'd like to blame the shallowEqual mentioned above
no sure what you mean by extra closures, talking about the Or was it the initial value of |
Hm weird indeed
|
I though there was a reason at first mobx did this, when calling the setState's dispatcher setRef({}) // executes on render once at the end of the batch
setRef(() => ({})) // executes on render as many times as it's called during the batch so but after testing, it seems it does not 🤔 function useForceUpdate() {
const [value, setRef] = useState(false);
return () => setRef(!value);
} I have updated my example |
Ok, my example was weak, we can see this mobx test failing about it while not using the react dispatcher instead of a direct value set |
This use case is far-fetched but in some contexts (gaming for example) the number of iterations can go beyond the maximum value supported by javascript and end up being `Infinity` which would cause the force update to stop working because `Infinity + 1 === Infinity` is true in javascript
I think the only reason was the need for previous value to perform increment :)
Toggle would rely on comparison happening immediately (rather than sometimes later), dunno if it's a safe choice... |
Pushed a PR #2983 as it was faster than explaining. |
thanks, I was wondering about the |
This use case is far-fetched but in some contexts (gaming for example) the number of iterations can go beyond the maximum value supported by javascript and end up being
Infinity
which would cause the force update to stop workingbecause
Infinity + 1 === Infinity
is true in javascriptI didn't added tests as I suppose we don't want to run a tests with a loop of 9007199254740991+ iterations and had some trouble mocking the tick value of
useState
Code change checklist
Updated/docs
. For new functionality, at leastAPI.md
should be updatednpm run perf
)