Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: update jt to add keys to react elements #237

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

morganney
Copy link

Addresses #166.

Adds an optional peer dependency on react and uses isValidElement and cloneElement to add keys to jt expressions that are React elements and do not already have a key defined.

Chose to bump the major version because this would be a breaking change for current users of jt who did not previously have react as a dependency.

Now you can safely use jt with JSX expressions without manually adding a key prop (which is an uncommon pattern when defining JSX outside of .map).

@@ -40,6 +41,14 @@ export const buildArr = (strs, exprs) => {
}, []);
};

export const addReactKeysWhereNeeded = (item, idx) => {
if (isValidElement(item) && !item.key) {
return cloneElement(item, { key: idx.toString() });
Copy link
Contributor

Choose a reason for hiding this comment

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

https://react.dev/learn/rendering-lists do not use array index as key

Copy link
Author

@morganney morganney Jun 25, 2024

Choose a reason for hiding this comment

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

You know that react.dev wasn't even around when this PR was opened right? I don't disagree with what you're saying, and I regularly point to the same warning, but we should remember context.

Is this still an issue with ttag, I haven't used it in years? If it is, then did you have an alternative suggestion for the key? One thought that comes to mind is the actual text content of the element, but there is no way to guarantee those will be unique on the same page. You could also combine the text content with the array index.

However, I would argue that in this case there is probably nothing wrong with using the array index, as React itself uses that when no explicit key is provided. It mainly becomes an issue if you are going to sort the items, or delete/insert items, which usually isn't a concern for translation of strings for the purpose of i18n.

Copy link
Contributor

Choose a reason for hiding this comment

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

No idea if this is an issue, just stumbled upon the PR and out of curiosity looked at the content. Didn't notice the PR was that old, and honestly don't have a better idea. But hey, isn't pointing at peoples misstakes the whole point with the internet!?! 😄
btw, it's been in the docs for a while https://17.reactjs.org/docs/lists-and-keys.html#keys 😜

Copy link
Author

Choose a reason for hiding this comment

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

Yes, I've read this on the old docs too. I still feel like when it comes to translation strings which usually are not dynamic in nature, so the reordering concern doesn't really apply, an index is an acceptable key in this context.

I'm more interested in whether this is still an issue with ttag. I know when I has to use this lib for i18n due to work requirements, it was a real pain to see these errors come up in the console, it lowered the signal-to-noise ratio. Maybe I'll rebase with master and see if this is still a thing. Looks like the original issue is still open.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants