-
Notifications
You must be signed in to change notification settings - Fork 76
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
Todomvc preact 02 #108
Todomvc preact 02 #108
Conversation
cc @julienw |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit sad because I liked the previous version you wrote (with useReducer
) :-) I would love to understand why that version was that much slower, despite having some more memoization... Or was it because of the memoization?
This version looks a lot more like the previous implementation and seems to behave somewhat similarly. One difference being that there's no more bound functions like we had before (I know Firefox was suffering from those). For example I think that things like handleSubmit = () => {
were generating bound functions after transpiling.
It would be good to make sure we have at least one react-like implementation with bound functions because we have them in real-life apps for sure.
Otherwise I left a few comments, tell me what you think :-)
|
||
addEventListener("hashchange", handleHashChange); | ||
handleHashChange(); | ||
}, []); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of adding a comment explaining that with this empty dependency array, this runs only once at mount time? For the folks who'll be reading this and don't know hooks well (like myself before this patch :D).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
} | ||
|
||
function handleDoubleClick() { | ||
setEditing(true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the autofocus behavior on the double click.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also miss the behavior for the blur
event. But that can be done in a follow-up if you prefer. (same for autofocus)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what behavior is missing for blur?
} | ||
|
||
function getTodos() { | ||
return [...todos]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a good reason to get a copy of the todos information besides defensive programming?
Note that we're calling it twice in app.js
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for the reason you mentioned.
At least in our tests I didn't see a difference in performance when I just returned the array vs returning a copy.
|
||
return ( | ||
<section class="main"> | ||
<input class="toggle-all" type="checkbox" onChange={onChange} checked={activeTodoCount === 0} /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem to be displayed, or am I missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@julienw - regarding the use of Date.now() - I tried previously two things:
the above mentioned forceUpdate solution from the doc
using useReducer comes with a cost and performance degraded in preact
using something simpler like val +1
this worked when running the workload by itself, but when running it in a test in speedometer it broke.
My assumption is that Date.now() produces a more unique number and ensures that useState doesn't get called with the same value for some reason and therefore prevents an update.
I could be totally off though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you replied to the wrong comment :)
using useReducer comes with a cost and performance degraded in preact
🤔 this is strange but I don't know the hooks enough (nor preact) to understand why this might be.
using something simpler like val +1
This should work if you use the function style of setting the state: setValue(val => val+1)
(in this case React will call the function with the value as a parameter) instead of setValue(value + 1)
where value
comes from useState
(in this case we may have an outdated value because of some race).
But no need to spend too much time on this, this is some details. This could also be looked at in follow-ups, we don't need to make it perfect in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the link you mentioned above shows the usage of useReducer.
https://reactjs.org/docs/hooks-faq.html#is-there-something-like-forceupdate
I did test the solution provided on that link and saw that the time to run increased.
regarding your other note - I actually tested it and saw it failing the test run.
Speedometer just stops
It's hard to compare React with Preact since they might implement things slightly differently.
* If the input field is present, we set focus programmatically. | ||
*/ | ||
useEffect(() => { | ||
const input = document.querySelector(".editing"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@julienw - I tested using useRef and it increased the time.
This works, but it's not recommended maybe?
We can either:
- useRef and be ok with the increased time
- keep this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nm - it does break - pushing up a version with useRef.
Note - there is a tiny performance hit, but the only solution that seems to work cross-browser.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again! There are a few small changes / follow ups called out in the thread. Could you please make sure we don't lose track of those?
To be clear, this can land without addressing them - just want to be sure we get to some kind of resolution to them before we ship. The ones I'm thinking of in particular are
Thorsten and I briefly chatted about this and it may make sense to do an accessibility audit for all of the todo apps (as opposed to one-off fixing this issue in the Preact variation). That would of course be done as a follow up. |
Issue filed: #112 |
Last set of updates 🤞 !
scores: https://gist.github.com/flashdesignory/212dea03f01a9fc0f6106199aadbe005
cc: @kara