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.

vdom manipulation, confusion ..

See original GitHub issue

https://stackblitz.com/edit/js-o4semn Code and inspected result would tell the problem. But I’ll try to explain. I’m trying to achieve , a table component, which creates rows and cols automatically with given children like:

<MTable>
    <th>x</th>
    <th>Test</th>
    <th>Test</th>

    <input snr type="text" />
    <input type="text" />
    <input type="text" />
</MTable>

As you might see in the code in the link above, in render of the MTable I’m traversing children, if there isn’t a row ( tr ) or a “snr -> short for start new row” prop adding it, and continue. The thing that confuses me is , some extra -unwanted- rows in inputs ( you could see them with react dev tools or chrome dev tools ) appear , this breaks code in more complex scenarios . bgxx

I may made a mistake in render code - if so forgive me - but if code is correct there must be something wrong about vnodes.

For completeness I’m adding component code :

const T = (p) => {
  const FieldTypes = ['input',];
  let currentrow = null;
  let thcurrentrow = null;
  let thead = null;

  let els = p.children.reduce(
    (res, child) => {
      let nodeName = getNodeName(child);
      let ca = child.attributes || {};
      if (FieldTypes.includes(nodeName)) {
        if (!currentrow || ca.snr) {
          currentrow = <tr ><td>{child}</td></tr>
          res.push(currentrow);
        }
        else if (currentrow) {
          currentrow.children.push(<td>{child}</td>);
        }
        return res;
      }
      else if (nodeName == "th") {
        if (!thead) {
          thead = <thead></thead>;
          res.push(thead);
        }
        if (!thcurrentrow) {
          thcurrentrow = <tr >{child}</tr>
          thead.children.push(thcurrentrow);
        }
        else if (thcurrentrow) {
          thcurrentrow.children.push(child);
        }
        return res; // added hope to fix, probably unnecessary ..
      }
      return res;
    }, []);

  return (

    <table>{els}</table>
  );
};

const getNodeName = c => c.nodeName ? (c.nodeName.name ? c.nodeName.name.toLowerCase() : c.nodeName.toLowerCase()) : "";

I’ve tried same -almost ( had to change some thing because of reacts dictation ) - code with react, and it worked. So I can conclude that the logic in render code is correct and this is a bug about vdom.

https://stackblitz.com/edit/react-73igvk?file=MTable.js

rct

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
marvinhagemeistercommented, Oct 10, 2018
1reaction
developitcommented, Oct 10, 2018

React is just ignoring children of <input> because the DOM doesn’t accept them.

It seems like you’re inadvertently mutating VNodes in-place, which is going to produce unexpected results.

If you need to replace the children or props/attributes of an element (child), use cloneElement():

import { cloneElement } from 'preact';

child = cloneElement(child, null, newChildren);

It’s best to do so with a fully constructed array of children, rather than mutating the element’s children in-place.

Read more comments on GitHub >

github_iconTop Results From Across the Web

vdom manipulation, confusion .. #1210 - preactjs/preact - GitHub
For completeness I'm adding component code : const T = (p) => { const FieldTypes = ['input',]; let currentrow = null; let thcurrentrow...
Read more >
React Virtual DOM vs Real DOM - Medium
If you landed here then you have an idea that React uses Virtual DOM for better performance and want to have better understanding...
Read more >
Why is React's concept of Virtual DOM said to be more ...
The representation of ReactJS's DOM manipulation seems little off to me. The ReactJS's virtual DOM is the one which changes entirely, not the...
Read more >
Shadow Dom vs. Virtual Dom: A Web Developer's Guide
“Shadow DOM vs. virtual DOM,” for instance, is quite a common confusion among developers, especially junior ones. In short, the shadow DOM ...
Read more >
Virtual DOM - React - Codecademy
Think of manipulating the virtual DOM as editing a blueprint, as opposed to moving rooms in ... The virtual DOM is not to...
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