-
Notifications
You must be signed in to change notification settings - Fork 421
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
docs(jsx): add explanation of automatic key insertion #1330
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
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.
When reading the new section I asked myself how the given example is different from the one just above it:
render() {
return (
<div>
{this.todos.map((todo) => (
<div key={todo.uid}>
<div>{todo.taskName}</div>
</div>
))}
</div>
)
}
Is this example outdated now since Stencil adds keys automatically?
hmm probably I need to explain better, I'll re-word a bit. In short it's not outdated, because we decided in the implementation of ionic-team/stencil#5143 not to insert keys into JSX nodes which are within curly braces (a JSX expression) because there are just so many possibly things that could be going on there that it's very hard to know when it would be safe to add keys to elements without breaking the user's expectation for how their component should render. An example would be a usage of render() {
return (
<div>
{this.todos.map((todo) => (
<div>
<div>{todo.taskName}</div>
</div>
))}
</div>
)
} and we inserted keys automatically the transformed code would look something like this: render() {
return (
<div>
{this.todos.map((todo) => (
<div key="a-key">
<div>{todo.taskName}</div>
</div>
))}
</div>
)
} this is because the transformation is done at compilation-time and on the basis of the syntax tree nodes that the transformer finds, not at runtime based on the actual data found in
the 2nd and 3rd are necessary for us to avoid because in each we can't assume that the user's intent is for the two (or more) different branches in 'return behavior' to be matched up, and inserting keys in that situation would change the rendering behavior for a lot of cases. Anywhoo - hopefully that helps explain a bit of how this works, I'll update to clarify more what is meant by the transformation not happening within a JSX expression |
Thanks @alicewriteswrongs , that clears things up! |
@christian-bromann I updated this a bit, lmk what you think! |
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 👍
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 - one minor suggestion that's non-blocking
{this.disabled && ( | ||
<div /> | ||
)} |
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 we could strengthen this section a little bit by making it clearer that it's in curly braces whereas the "slot-wrapper" is not.
I think we could accomplish that by:
- Adding whitespace (newlines) after the opening
{
and before the closing}
- Adding a text node and/or id to the div that helps draw attention to it being in curlies:
{this.disabled && ( | |
<div /> | |
)} | |
{ | |
this.disabled && <div id="in-curly-braces">Won't receive automatic key insertion</div> | |
} |
☝️ That might not be the best in terms of presentation (particularly may be too wide for the viewport), but is the general idea. Thoughts?
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.
mmm I think I'll go for something like
{ this.disabled && <div id="no-key">no key!</div> }
just to make sure it's narrow enough
b7de9c6
to
f9012ec
Compare
This functionality was added to Stencil in ionic-team/stencil#5143
f9012ec
to
9525183
Compare
Add some documentation to our page on JSX which explains the changes made in ionic-team/stencil#5143