Preact: Preact remounts nodes when a child element is perpended

Created on 11 Nov 2016  路  6Comments  路  Source: preactjs/preact

Hello!

I have an issue with Preact where Preact remounts my nodes in the DOM whenever a new node is prepended(an ELEMENT_NODE, not TEXT_NODE). This causes css transitions to break.

If my DOM tree looks like this:

<div id="parent">
  <div id="childA"></div>
  <div id="childB"></div>
</div>

And i then prepend a child via a Preact component, it will now look like this:

<div id="parent">
  <div id="newChild"></div>
  <input type="checkbox" id="childA-with-transition"></div> // removed and then placed into dom again
  <div id="childB"></div> // removed and then placed into dom again
</div>

This is troublesome because CSS transitions are broken. Let's say i have a smooth transition on my checkbox and as i check the checkbox something is prepended(It could be an error message) then the css-transition would be interrupted because the DOM node is remounted.

Watch(in chrome debugger) as the <input> element is remounted into the DOM whenever <span>Required field</span> is prepended. This example has react aliased to preact-compat:

import React from 'react';

class Field extends React.Component {
  constructor () {
    super();
    this.state = {checked: false};
    this.onChange = this.onChange.bind(this);
  }
  onChange () {
    this.setState({checked: !this.state.checked});
  }
  render () {
    const {checked} = this.state;
    return (
      <div>
        {checked ? '' : <span>Required field</span>}<br />
        <input type="checkbox" onChange={this.onChange} value={checked} />
      </div>
    )
  }
}

export default Field;
bug has fix important

All 6 comments

Interesting issue - looking into it! It'd be solvable by adding keys, but I think that's inelegant. I'll let you know if I find something.

Facing the same issue

Ack! Looking into it! It's something to do with the way TextNodes are reconciled.

This is my current top issue. Will be fixed ASAP!

Sweet! I'd love to see how you solve it so that i can get a better understanding of how the vdom in Preact works

Have a fix incoming in the beta tag :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

matthewmueller picture matthewmueller  路  3Comments

simonjoom picture simonjoom  路  3Comments

SabirAmeen picture SabirAmeen  路  3Comments

adriaanwm picture adriaanwm  路  3Comments

kay-is picture kay-is  路  3Comments