Can't use keys on custom components.
See original GitHub issueFor now if you try to specify a key
for JSX component it won’t be actually used by hyperapp’s diff/patch algorithm:
const state = {
posts: [
'Hello',
'world'
]
}
const actions = {}
const Post = (props, children) => <h1>{children}</h1>
const view = (state, actions) => (
<main>
{state.posts.map(post =>
<Post key={post}>{post}</Post> // <= the `key` prop is not used by diff algorithm
)}
</main>
)
app(state, actions, view, document.body)
console.log(view(state, actions)) // =>
// { name: 'main', props: {}, children: [
// { name: 'h1', props: { no key here }, children: ['Hello'] },
// { name: 'h1', props: { no key here }, children: ['World'] }
// ] }
Demo: https://codepen.io/frenzzy/pen/dJmEOm?editors=0010
Let’s discuss, should we automatically pass the key
down into actual virtual DOM node or user must do it manually? How not to forget to do it?
Issue Analytics
- State:
- Created 6 years ago
- Comments:22 (21 by maintainers)
Top Results From Across the Web
Reactjs - Keys on custom components in list - Stack Overflow
1) Custom components can't have a key attribute, as key is a special property used by React and is not passed to the...
Read more >Key press issue with custom Control #4279 - GitHub
I'm using react-select v3.1 and I'm experiencing some very odd behavior, not sure if I'm doing something wrong or if there is a...
Read more >How To Create Custom Components in React - DigitalOcean
Custom components are independent pieces of functionality that you can reuse in your code, and are the building blocks of all applications ...
Read more >Using custom elements - Web Components | MDN
One of the key features of the Web Components standard is the ability to create custom elements that encapsulate your functionality on an ......
Read more >Extend the OmniScriptBaseMixin Component - Salesforce Help
Custom Lightning web components built outside of the package cannot use any ... The omniSaveState function accepts three arguments: a data object, a...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@frenzzy You can easily achieve this behavior, by defining your own “higher order” version of the
h
function, such as like this:You could augment that to also pass along lifecycle events but you’d have to take care to compose them with any lifecycle events the component itself might define on the root node
Edit: On closer reading, maybe the above was already clear. Now we’re discussing wether to add this behavior into the actual
h
, is that right?Sounds like @frenzzy is asking to pass down the key property automatically.
This is as far as I know a default behavior in Vue. All properties given to a component are passed down to the root child (a component must have a root child == no component that return an array of children).
But this behavior is not really hard to get in userland. I personnaly took the habit to pass down all properties.
This basically allow
Component
user to set class property when it is needed and the class will be set on the root child of the component.