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.

Initing child components with no associated dom

See original GitHub issue

Hi!

in several components I find myself doing few workaround to accomplish the child components be a simple data representation.

Example 1 - Table

<vk-table>
  <vk-table-column header="Name">Cell Content</vk-table-column>
  <vk-table-column header="Name">Cell Content</vk-table-column>
</vk-table>

The vk-table-column purpose is to collect the data but leverage the rendering to vk-table which will iterate over the children as necessary to accomplish the Header, Rows, Cell combination.

Example 2 - Tabs

<vk-tabs>
  <vk-tab label="Name">Tab Content</vk-tab>
  <vk-tab label="Name">Tab Content</vk-tab>
</vk-tabs>

Similar as in previous example but additionally the Tabs content should be rendered using single Transition.

To the point

In both examples, the children can’t be used out the default way to accomplish the special rendering. There are several workarounds but are tricky, unsupported and of course not best practice, so I would like to start a discussion about solving this particular need.

A solution could be having a way to initiate those $children with no render or template set as out of document components. They would be accessible early in the parent for immediate use during rendering.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
amrdrazcommented, Nov 9, 2018

I found this issue by searching what is the this._l thing (still don’t have an answer though).

I think that I want to implement exactly the same thing.

I like the way how Table component from element library can be used - a very straightforward. So I wanted to implement something similar but my own and with less features, well you know… 😃 Just some simple table component with neat api.

But I was really confused by reading element source code, there are plenty of jsx and some this._l, this._renderProxy. And it seems like there is just no way to achieve similar component interface without all those hacks.

@Alendorff as far as I understood Vue.prototype._l is a short hand for renderList the helper used by v-for

so vue template code

<td v-for="(column, index) in columns" calss="table_cell" />

in jsx would be

{ _l(columns, (column, index) => <td calss="table_cell" /> ) }
1reaction
miljan-aleksiccommented, Dec 23, 2016

Ok, let me put all the cards on the table. This is an adapted extract of a real component workflow:

VkTable

<template>
<table
  { this.$slots.default }
  <thead>
    <tr>
      { this._l(this.$children, col => col.headerRender.call(col._renderProxy, h)) }
    </tr>
  </thead>
  <tbody>
    { this._l(this.data, (row, rowIndex) =>
      <tr>
        { this._l(this.$children, col =>
          col._cellRender.call(col.renderProxy, h, { row, rowIndex })
         ) }
      </tr>
    )}
  </tbody>
</table>
</template>

VkTableColumn

render (h) {
  return (<col></col>)
},
headerRender (h) {
  const scopedSlot = this.$scopedSlots && this.$scopedSlots.header
  return (<th>{ scopedSlot ? scopedSlot() : this.header }</th>)
},
cellRender (h, { row, rowIndex }) {
  const cell = this.cell
  const scopedSlot = this.$scopedSlots && this.$scopedSlots.cell
  return (<td>{ scopedSlot ? scopedSlot({ row, rowIndex }) : row[cell] }</td>)
}

VkTableColumnSelect

extends: VkTableColumn,
headerRender (h) {
  // using a similar approach as default renders instead a Selectable Checkbox
},
cellRender (h) {
  // using a similar approach as default renders instead a Selectable Checkbox
}

Usage

<VkTable>
  <VkTableColumnSelect />
  <VkTableColumn header="Simple Header" cell="dataKey" />
  <VkTableColumn>
    <template slot="header" scope="props">Custom Header inner content</template>
  <VkTableColumn>
<VkTable>

Conclusions

  • The above is only possible because VkTable process the $slots.default, with no other reason but to make the $children inited.
  • The passed in data is complex enough to make the use of Array prop obsolete (that was my first approach and is far from good).
Read more comments on GitHub >

github_iconTop Results From Across the Web

Aura: init of child component executes before parent ...
I have to pass parameters to a component (Aura Table) inside parent component (no inheritance), Unfortunately child component's init is ...
Read more >
How do I access refs of a child component in the parent ...
We create Components that forward their ref with React.forwardRef . The returned Component ref prop must be of the same type as the...
Read more >
The Component Lifecycle - Ember Guides
Listed below are the component lifecycle hooks in order of execution according to render scenario. On Initial Render. init; didReceiveAttrs; willRender ...
Read more >
Angular @ViewChild: In-Depth Explanation (All Features ...
Using @ViewChild to inject a reference to a DOM element. Instead of injecting a direct child component, we might want to interact directly...
Read more >
ASP.NET Core Razor component lifecycle | Microsoft Learn
The C# methods associated with the lifecycle events are defined with ... as JS interop calls that interact with the rendered DOM 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