-
Notifications
You must be signed in to change notification settings - Fork 23
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
Unique ids for all icon elements referenced through url(#someId)
#5
Conversation
My apologies for the huge diffs. I had to convert the bodies of the components from expressions to code block so that I could use |
These are the changes I made:
|
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.
Hi @baremetalfreak!
Thanks for this great improvement!
Just left some small remarks 🙂
let counter = 0; | ||
|
||
const useUniqueId = () => { | ||
return useMemo(() => counter++, []); |
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.
You can make this an implicit return
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.
Also: shouldn’t counter
be the dependency of useMemo
? 🤔
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.
Not really. Hooks are executed during render and their dependencies should come from properties and state. There is no way for react to know when to re-evaluate a hook if something outside of its control has changed.
This hook is essentially a poor man's hack to facebook/react#1137 . I have not found a cleaner way to do it.
Do you want me to add some comment into the code to explain it?
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 we want is a unique identifier for every "instance" of a component. That is achieved by the useMemo
's second argument []
which says, execute the function and return new, incremented, value on every mount. And a mount is when a new "instance" is created.
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.
Does that make sense @MichaelDeBoey ?
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.
Maybe adding a little comment here would make this a bit clearer indeed. 🙂
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.
273a194
to
88c40a7
Compare
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.
LGTM
Is there something you would like me to do with this PR before merging it? |
Hey @baremetalfreak, thanks for the PR! Sorry it took me so long to get around to looking at it, but it looks great to me! I'll merge this and publish a new version this evening. Love that you added tests as well! |
All elements that use
id
attribute in icons now generate a unique identifier for every mount of the component. It fixes #4.