question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't use keys on custom components.

See original GitHub issue

For 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:closed
  • Created 6 years ago
  • Comments:22 (21 by maintainers)

github_iconTop GitHub Comments

4reactions
zacenocommented, Jan 11, 2018

@frenzzy You can easily achieve this behavior, by defining your own “higher order” version of the h function, such as like this:

import {h as _h, app} from 'hyperapp'

const h = (tag, props, children) => {
    if (typeof tag === 'function') {
        const node = tag(props, children)
        node.props.key = props.key
        return node
    } else {
        return _h(tag, props, children)
    }
}

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?

3reactions
Swizzcommented, Jan 11, 2018

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.

const Component = (props) => (
  <section {...props}>
    <h1>Hi.</h1>
  </section>
)

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.

const view= (state, actions) => (
  <main>
    <Component class="bg-red"/>
  </main>
)
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found