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.

Component ignores the "key" attribute when it is the only child of another Component

See original GitHub issue

When a component returns in render() methon only one of its chilren, for example:

class Router extends Component {
  render({ url, children }) {
    return children.find(c => c.attributes.path === url);
  }
}

it ignores the key attributes from component’s children. And then children of same type are patched in place by virtual dom algorithm instead of mounting / unmounting. Like if there are no key attributes at all. I now that Preact recycles DOM nodes and even components instead of render them again. But if we modify code like that:

class Router extends Component {
  render({ url, children }) {
    return (
      <div>
        { children.find(c => c.attributes.path === url) }
      </div>
    );
  }
}

then everethyng works fine and components are unmounted. Fore details please see JSFiddle

And it seems that in React this bug is not present. See JSFiddle

Why it is matter for me? Because I want to port my application from KnockoutJS to Preact. Also I need some routing setup. And now I end up with followig code:

import { h, Component, render } from "preact";
import { Router } from "preact-router";

class KnockoutPage extends Component {
  shouldComponentUpdate = () => false;
  componentDidMount() {
    // initialize Knockout component
  }
  componentWillReceiveProps({ url, matches }}) {
    // transfer props to Knockout component
  }
  componentWillUnmount() {
    // dispose Knockout component
  }
  render({ name }) {
    let binding = `component: { name: '${ name }', params: $rawData }`
    return <div data-bind={ binding }></div>
  }
}

route((
  <Router>
    <KnockoutPage key="1" path="/foo" name="foo-page-component" />
    <KnockoutPage key="2" path="/bar" name="bar-page-component" />
  </Router>
), document.body);

P.S. Sorry for my poor English

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
developitcommented, Dec 2, 2016

Released the fix! http://jsfiddle.net/developit/zckmzj51/5/

Sorry about the long wait on this one @gnaeus and thanks for your patience!

1reaction
developitcommented, Sep 7, 2016

@gnaeus I think the SSR fix in 6 bypassed this change. Re-opening to look into it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Missing key for a child element in a list, although assigned
key is an internal react property used to help re-render components. It will not display in the DOM. Use another property if you...
Read more >
Using React's Key Attribute to remount a Component - Nik Graf
Using React's Key Attribute to remount a Component. Usually we use React's special "key" string attribute only in combination with Lists.
Read more >
Warning "Invalid attributes detected on template" in nested ...
After Summer '22, an LWC component doesn't render <template> elements and its children if it includes an invalid attribute or directive.
Read more >
"key" attribute can be used outside of iteration #2697 - GitHub
Only use the key attribute inside of an iteration. ... I would suggest that the compiler ignores the key directive when the parent...
Read more >
Testing Components with children - Testing Angular
There are two fundamental ways to test Components with children: ... Or tell Angular to ignore the unknown elements.
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