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.

Feature: keep existing DOM attributes

See original GitHub issue

I have a use case where I’d like to keep the existing attributes on the elements, when rendering a Component into the DOM.

Here’s an example:

Existing DOM node:

<div id="outer" data-foo="foo">
  <div id="inner" data-bar="bar"></div>
</div>

Component:

const TestComponent = () => <div class="outer">
  <div class="inner">
    Test component
  </div>
</div>;

Expected output:

<div id="outer" data-foo="foo" class="outer">
  <div id="inner" data-bar="bar" class="inner">
    Test component
  </div>
</div>

This did work in Preact 10.0.0, but not anymore in 10.0.1, as mentioned in this issue, so it look like this was actually a bug in 10.0.0?

Codepen with Preact 10.0.0: https://codepen.io/dioslaska/pen/mdebjga Codepen with latest Preact: https://codepen.io/dioslaska/pen/jObNpdP

If yes, could you point me in the right direction what exact changes were made to avoid this? I could maybe use a custom preact version, if this won’t be possible in the future. Also, I can’t stick with 10.0.0 either, since there are other bug present as well.

A little details on the use case to explain why would be this useful: I’m working on a UI library supporting multiple frameworks. We need to support plain Javascript / jQuery as well, and the approach is to re-use the React components, and render them into the DOM using Preact.

A real world example, which enhances an existing input

<label id="my-label">
  <input id="my-input">
</label>

The Preact component will be rendered in the place of the label element. This works fine, but starting from Preact 10.0.1 the ids and other attributes are removed from the pre-existing elements. This is not ok, since the users of the library should be able to access the elements, e.g. using getElementById, to add event listeners, etc.

Please let me know if there’s any solution for this, or plans on changing this, or, in worst case, what could be modified in the preact source to make this work, and what are the potentially unwanted side effects.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:23 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
andrewigginscommented, Jan 5, 2021

@dioslaska Another workaround to consider is to first call hydrate then render. hydrate will claim the existing DOM elements and then render will ensure that any differences between your VNodes and the DOM nodes are patched. https://codepen.io/andrewiggins/pen/yLajMrw

1reaction
developitcommented, Apr 8, 2020

Here’s a workaround for hydrate that lets you specify the replaceNode to hydrate:

import { hydrate } from 'preact';

const toReplace = document.getElementById('inner');
hydrate(<App />, { childNodes: [toReplace] });
Read more comments on GitHub >

github_iconTop Results From Across the Web

Attributes and properties - The Modern JavaScript Tutorial
Attributes and properties ... When the browser loads the page, it “reads” (another word: “parses”) the HTML and generates DOM objects from it....
Read more >
DOM Attributes in React 16
No. We don't encourage you to keep data in DOM attributes. Even if you have to, data- attributes are probably a better approach, ......
Read more >
How To Modify Attributes, Classes, and Styles in the DOM
In this tutorial, we will learn how to further alter the DOM by modifying styles, classes, and other attributes of HTML element nodes....
Read more >
Chapter 12. Creating and Removing Elements and Attributes
You need to add a new div element to the web page before an existing div element. Solution. Use the DOM method createElement...
Read more >
Custom Element Best Practices - web.dev
Custom elements let you construct your own HTML tags. This checklist covers best practices to help you build high quality 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