Skip to content
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

Merged
merged 4 commits into from
Aug 23, 2019

Conversation

VitGottwald
Copy link
Contributor

@VitGottwald VitGottwald commented Aug 14, 2019

All elements that use id attribute in icons now generate a unique identifier for every mount of the component. It fixes #4.

@VitGottwald
Copy link
Contributor Author

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 useUniqueId. I used prettier to format it.

@VitGottwald
Copy link
Contributor Author

VitGottwald commented Aug 15, 2019

These are the changes I made:

  • created useUniqueId.tsx custom hook to generate new id on every mount
  • updated all icons that define elements with id="someId" and use url(#someId) to refer to it - appended _${id} in all such places
  • written tests to
    • make sure all elements with the id attribute id="someId" have different ids
    • validate that two renders of the same icon generate unique ids

Copy link
Contributor

@MichaelDeBoey MichaelDeBoey left a 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++, []);
Copy link
Contributor

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

Copy link
Contributor

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? 🤔

Copy link
Contributor Author

@VitGottwald VitGottwald Aug 15, 2019

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?

Copy link
Contributor Author

@VitGottwald VitGottwald Aug 15, 2019

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.

Copy link
Contributor Author

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 ?

Copy link
Contributor

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. 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@MichaelDeBoey MichaelDeBoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@VitGottwald
Copy link
Contributor Author

Is there something you would like me to do with this PR before merging it?

@Saeris
Copy link
Contributor

Saeris commented Aug 23, 2019

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!

@Saeris Saeris merged commit 0a1b616 into codesandbox:master Aug 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Node and Unibit icons not shown properly in Container Templates tab
3 participants